我的表是:
id val catid
1 title1 7
2 text1 8
3 title2 7
4 text2 8
我希望将行组合为:
val1 val2
title1 text1
title2 text2
我使用了这个查询:
select
(case when catid = 7 then val end) val1,
(case when catid = 8 then val end) val2
from mytable
但我的结果是:
val1 val2
title1 Null
Null text1
title1 Null
Null text2
如何解决? 感谢
答案 0 :(得分:1)
使用GROUP_CONCAT
函数怎么样?也许你可以通过用逗号分割它来在你的程序逻辑中使用它。
select group_concat(val) as vals
from table group by catid
在你的应用中:
splitted_array = vals.Split(",");
key=splitted_array [0];
value=splitted_array [1];