gpt4 book ai didi

mysql - 如何使一条记录的值成为mysql上字段的标题?

转载 作者:行者123 更新时间:2023-11-29 10:04:22 25 4
gpt4 key购买 nike

我的查询是这样的:

SELECT a.number, a.description, b.attribute_code, b.attribute_value 
FROM items a
JOIN attr_maps b ON b.number = a.number WHERE a.number = AB123

如果执行查询,结果如下:

enter image description here

我想要这样的结果:

enter image description here

我该怎么做?

最佳答案

您可以使用条件聚合:

SELECT i.number, i.description,
MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
attr_maps am
ON am.number = i.number
WHERE i.number = AB123
GROUP BY i.number, i.description

关于mysql - 如何使一条记录的值成为mysql上字段的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com