在 SQLite 中获取一个字符串中的所有行 ID
Get all row ids in one string in SQLite
我在 SQLite 中有一个 table,其中包含一些数据。
像这样
Table id
1
2
3
4
5
6
7
我想要的是一个字符串,其中包含所有由分号 (;
) 字符分隔的 ID,例如
1;2;3;4;5;6;7
我知道有组连接。
但我不知道如何用这个样本来做。
我发现了另一种名为 COALESCE 的方法 here,但在 SQLite 中我们不能声明变量。
在 SQLite 中有什么方法可以做到这一点吗?
可以使用聚合函数group_concat(X,Y)
来实现:
The group_concat() function returns a string which is the
concatenation of all non-NULL values of X. If parameter Y is present
then it is used as the separator between instances of X. A comma (",")
is used as the separator if Y is omitted. The order of the
concatenated elements is arbitrary.
SELECT GROUP_CONCAT(id, ';') AS result
FROM tab_name
-- WHERE id < 10; -- if needed
如果您不指定 GROUP BY ...
子句,所有记录将被视为一组。
我在 SQLite 中有一个 table,其中包含一些数据。
像这样
Table id
1
2
3
4
5
6
7
我想要的是一个字符串,其中包含所有由分号 (;
) 字符分隔的 ID,例如
1;2;3;4;5;6;7
我知道有组连接。
但我不知道如何用这个样本来做。
我发现了另一种名为 COALESCE 的方法 here,但在 SQLite 中我们不能声明变量。
在 SQLite 中有什么方法可以做到这一点吗?
可以使用聚合函数group_concat(X,Y)
来实现:
The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.
SELECT GROUP_CONCAT(id, ';') AS result
FROM tab_name
-- WHERE id < 10; -- if needed
如果您不指定 GROUP BY ...
子句,所有记录将被视为一组。