如何解决根据第二列中的值从单个列中选择两个字段
| 我是数据库新手,所以请原谅我一百万遍,如果您尝试执行此操作,则找不到解决方案。 我有一张表-我们将其称为\'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
这可能对您有帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。