sql – 如何获得每行固定数量的结果?

如何使用sqlite3数据库使用此格式构建查询?

CREATE TABLE sample (integer foo);
INSERT INTO sample VALUES (1);
...
INSERT INTO sample VALUES (10);

结果的格式

1,2,3
4,5,6
7,8,9
10

最佳答案 您必须添加一些标准来对它们进行分组:

CREATE TABLE sample (integer foo, char(1) bar);
INSERT INTO sample VALUES
(1, 'a'), (2, 'a'), (3, 'a'), (4, 'b'), (5, 'b'), (6, 'b'), (7, 'c') ...;

SELECT GROUP_CONCAT(foo ORDER BY foo, ',')
FROM sample
GROUP BY bar

编辑:

试试这个:

select group_concat(foo) from (
  select s1.foo, (count(*) - 1) / 3 grp from sample s1
  join sample s2 on s1.rowid >= s2.rowid
  group by s1.rowid
) final
group by grp
点赞