我想在一个中显示多行。我把它作为输出:
name | value
------------
A | 1
A | 2
A | 3
B | 2
B | 4
C | 6
我想要:
name | value
------------
A | 1,2,3
B | 2,4
C | 6
我当前的查询是这样的:
SELECT name, value
FROM table
ORDER BY name
这可能吗?
提前致谢。
答案 0 :(得分:-1)
试试这个:
create table tab(name varchar(50), val int);
insert into tab values ('A', 1), ('A', 2), ('A', 3), ('B', 2), ('B', 4), ('C', 6);
.mode column
.headers on
select name, group_concat(val, ',') as list from tab group by name order by name;
输出:
sqlite> select name, group_concat(val, ',') as list from tab group by name order by name;
name list
---------- ----------
A 1,2,3
B 2,4
C 6