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

问题描述

| 我是数据库新手,所以请原谅我一百万遍,如果您尝试执行此操作,则找不到解决方案。 我有一张表-我们将其称为\'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\'
    

解决方法

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
    ,
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
    ,
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
这可能对您有帮助     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...