【问题标题】:Two Columns, Same Data, Different Where Clauses两列,相同的数据,不同的 Where 子句
【发布时间】:2016-11-21 19:34:11
【问题描述】:

我需要使用两个不同的 WHERE 子句语句查询同一列。

我正在使用 Magento,但无法更改表格格式。

表格视图:catalog_product_entity_varchar

entity_id | attribute_id | value                |
5         | 60           | Bear Apprentice      |
5         | 86           | bear-apprentice      |
5         | 71           | Item Description     |
5         | 74           | /a/p/apprentice2.jpg |

我想让它显示为:

entity_id | Item Name            |  img                   |
5         |  Bear Apprentice     | /a/p/apprentice2.jpg   |

最终使attribute_id 6074 出现在同一行但在两个单独的列中。 是否可以对列别名执行 WHERE 子句?

谢谢!

【问题讨论】:

  • 你知道attribute_id吗?
  • @Shaharyar 是的,我想在同一行的两个不同列中获取属性 ID 60 和 74。

标签: mysql sql magento


【解决方案1】:

在 mySQL 中,一种透视方法是使用 casemaxgroup by。这确实假设 entity_Id 只能有一个配对值(每个实体每个值 1 个属性),例如60 对于实体​​ 5 只能出现一次。

SELECT entity_ID
     , max(case when attribute_Id = 60 then value end) as `Item Name`
     , max(case when attribute_Id = 74 then value end) as img
FROM tableName
WHERE entity_ID = 5
GROUP BY entity_ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2012-04-11
    • 2021-09-14
    • 2014-10-04
    相关资源
    最近更新 更多