2016-11-21 57 views
0

我需要用两个不同的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子句?

谢谢!

+0

你知道'attribute_id'吗? – Shaharyar

+0

@Shaharyar是的,我想要在两个不同的列中获得attribute_id 60和74,但在同一行。 – hinteractive02

回答

2

在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 
相关问题