根据第二列中的值从单列中选择两个字段

时间:2011-04-14 21:25:54

标签: mysql

我是一个新手数据库,所以请原谅我,如果之前已经覆盖了一百万次,我找不到我正在尝试做的解决方案。

我有一张表 - 我们称之为'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'的'value'列中的所有项目拖到名为'Long Description'的列中,将同一列中带有'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'

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

这可能有助于你..