2011-04-14 108 views
2

我是一個數據庫newb,所以請原諒,如果這已被覆蓋了一百萬次之前,我沒有找到我想要的解決方案做。根據第二列中的值從單列中選擇兩個字段

我有一個表 - 我們將稱之爲'product_attributes',其中存儲了所有產品的許多特定屬性。在此表中,'attribute_id'告訴我該行中存儲了哪些類型的信息,'store_id'告訴我該信息顯示在哪個網站上,'entity_id'告訴我該信息是關於哪個產品,'value'是關於該產品。格式爲:

value_id entity_type_id attribute_id store_id entity_id value 
1221  4     57    0   306   Detailed Description of Product 
1222  4     58    0   306   Quick Overview of Product 
1223  4     68    0   306   metakeywords 
1224  4     89    0   306   metadescription 
1225  4     93    0   306   Other Stuff 
1226  4     57    0   307   Detailed Description of Product 
1227  4     58    0   307   Quick Overview of Product 
1228  4     68    0   307   metakeywords 
1229  4     89    0   307   metadescription 
1230  4     93    0   307   Other Stuff 

我需要運行一個查詢,把所有的項目從列「價值」與「attribute_id = 57」到一個名爲「長說明」一欄,並從同一列的所有項目與「attribute_id = 58'到另一個名爲「簡短描述」的列中。我能得到的值分別很容易的使用:

SELECT product_attributes.value FROM product_attributes WHERE attribute_id=57 

SELECT product_attributes.value FROM product_attributes WHERE attribute_id=58 

但我需要一個單獨的列中爲每個這樣的:

Long Description      Short Description 
Detailed Info of 'entity_id 306'  Overview of 'entity_id 306' 
Detailed Info of 'entity_id 307'  Overview of 'entity_id 307' 
+0

你需要在你的查詢中加入JOIN。 – sukru 2011-04-14 21:29:03

回答

0
SELECT pr1.value as LongDescription, pr2.value as ShortDescription 
FROM product_attributes pr1 
JOIN product_attributes as pr2 
WHERE pr1.attribute_id=57 AND pr2.attribute_id=58 
0
select a.value as 'long desc', b.value as 'short desc' 
from 
(SELECT entity_id, product_attributes.value FROM product_attributes WHERE attribute_id=57) a, 
(SELECT entity_id, product_attributes.value FROM product_attributes WHERE attribute_id=58) b 
where a.entity_id = b.entity_id 
0
select (SELECT a.value FROM product_attributes as a where a.attribute_id=57) as LongDescription,(SELECT b.value FROM product_attributes as b where b.attribute_id=58) as ShortDescription from product_attributes 

這可能會幫助你..

相關問題