我有一张这样的表
表名:attribute_table
id option_name_1 option_value_1 option_name_2 option_value_2
1 color Red size 10
2 color Blue size 10
3 size 12
4 size 14
5 size 16 color yellow
6 size 18 color yellow
结果应为
size contains: 10, 12, 14, 16, 18
color contains: Red, Blue, yellow
我的查询是
尺寸:
SELECT CASE WHEN option_value_2='' THEN
option_value_1 ELSE option_value_2 END AS optValue
FROM option_value_2 WHERE
(option_name_1='size' OR option_name_2='size' ) GROUP BY `optValue`
对于颜色:
SELECT CASE WHEN option_value_2='' THEN
option_value_1 ELSE option_value_2 END AS optValue
FROM option_value_2 WHERE
(option_name_1='size' OR option_name_2='size' ) GROUP BY `optValue`
但我没有得到正确的价值?
有什么办法吗?
由于
答案 0 :(得分:1)
E.g:
SELECT option_name
, GROUP_CONCAT(DISTINCT option_value ORDER BY CASE WHEN option_name IN('size') THEN option_value + 0 ELSE LENGTH(option_value) END) option_values
FROM
( SELECT id,option_name_1 option_name,option_value_1 option_value FROM attribute_table
UNION
SELECT id,option_name_2,option_value_2 FROM attribute_table
) x
WHERE option_name IS NOT NULL
GROUP
BY option_name;
答案 1 :(得分:0)
尝试尺寸:
select distinct option_value_1 from attribute_table where option_name_1 = 'size'
UNION ALL
select distinct option_value_2 from attribute_table where option_name_2 = 'size';
同样地,你可以用它做颜色。
答案 2 :(得分:0)
我认为正确的大小查询是:
SELECT distinct coalesce(option_value_1, option_value_2) AS optValue
FROM attribute_table WHERE
coalesce(option_name_1,option_name_2) ='size'
这也可以用于颜色。