如何在 MariaDB 的子查询中将结果集转换为 CSV 字符串?
How to turn a result set into a CSV string in subquery in MariaDB?
假设我在 Customers
table:
中有这些数据
Id,Name
1,John
2,David
3,Sophia
我想select这个数据,结果是这样的:
NamesCsv
John,David,Sophia
在SQL服务器中,我会写:
select
(
select [Name] + ','
from Customers
for xml path('')
) as NamesCsv
如何在 MariaDB 中实现?
GROUP_CONCAT()
将行连接成单个字段,但限制是结果不包含 NULL 值。
- 重复:
select group_concat(name) as NamesCsv from Customers;
- 没有重复:
select group_concat(distinct name) as Namescsv from Customers;
假设我在 Customers
table:
Id,Name
1,John
2,David
3,Sophia
我想select这个数据,结果是这样的:
NamesCsv
John,David,Sophia
在SQL服务器中,我会写:
select
(
select [Name] + ','
from Customers
for xml path('')
) as NamesCsv
如何在 MariaDB 中实现?
GROUP_CONCAT()
将行连接成单个字段,但限制是结果不包含 NULL 值。
- 重复:
select group_concat(name) as NamesCsv from Customers;
- 没有重复:
select group_concat(distinct name) as Namescsv from Customers;