简体   繁体   English

两列,相同数据,不同子句

[英]Two Columns, Same Data, Different Where Clauses

I'm needing to query the same column with two different WHERE clause statements. 我需要用两个不同的WHERE子句语句查询同一列。

I'm working with Magento and I'm unable to change the table format. 我正在与Magento合作,但无法更改表格格式。

Table View: catalog_product_entity_varchar 表格检视: 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 |

I would like to have it displayed as: 我希望将其显示为:

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

Ultimately getting attribute_id 60 and 74 to appear on the same row but in two separate columns. 最终, attribute_id 6074出现在同一行中,但出现在两个单独的列中。 Is it possible to do a WHERE clause on a column alias? 是否可以对列别名执行WHERE子句?

Thanks! 谢谢!

In mySQL one way to pivot is to use case and max and group by . 在mySQL中,一种透视的方法是使用casemaxgroup by This does assume an entity_Id can only have one paired value (1 attribute per value per entity) eg 60 can only appear once for entity 5. 这确实假设一个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM