我需要使用两个不同的 WHERE 子句语句查询同一列。
我正在使用 Magento,但无法更改表格格式。
表格 View :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 60
和 74
出现在同一行但在两个单独的列中。
是否可以对列别名执行 WHERE 子句?
谢谢!
最佳答案
在 mySQL 中,透视的一种方法是使用 case
和 max
以及 group 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
关于mysql - 两列,相同的数据,不同的Where子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727958/