我创建了以下查询,从3个不同的表中提取数据:
SELECT
product_searchables_names.class_name AS Class_Name,
product_searchables_names.label AS Attribute_Label,
jss_products.extrafield102 AS Attribute1,
jss_products.extrafield103 AS Attribute2,
jss_products.extrafield104 AS Attribute3,
jss_products.extrafield105 AS Attribute4,
jss_products.extrafield106 AS Attribute5,
jss_products.extrafield107 AS Attribute6
FROM
product_searchables_categories
JOIN product_searchables_names ON product_searchables_categories.searchableID = product_searchables_names.ID
JOIN jss_products ON product_searchables_categories.categoryID = jss_products.categories
WHERE
jss_products.productID = '134890'
ORDER BY
product_searchables_categories.searchable_order ASC
它以以下格式输出数据
但我需要这种格式,其中第1行从“Attribute”1中选择值,第2行从“Attribute2”中选择值,等等:
我该怎么做?
提前谢谢
最佳答案
你可以试试case语句
select
product_searchables_names.class_name AS Class_Name,
product_searchables_names.label AS Attribute_Label,
CASE
WHEN product_searchables_names.label = 'Resolution' THEN jss_products.extrafield104
WHEN product_searchables_names.label = 'Networked?' THEN jss_products.extrafield106
WHEN ...
END
from
product_searchables_categories
...
或使用类名而不是标签
关于mysql - MySQL多重连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14359247/