<分区>
我知道合并字段的方法
- MySQL:
CONCAT( )
- 甲骨文:
CONCAT( )
,||
- SQL 服务器:`+
但是... 我想在查询后合并,这可能吗?
标签 mysql sql sql-server oracle
<分区>
我知道合并字段的方法
CONCAT( )
CONCAT( )
,||
但是... 我想在查询后合并,这可能吗?
最佳答案
给你:
MySQL 使用 group_concat
:
select a.name,
a.opcode,
group_concat(month order by b.pk separator ', ') as months
from tablea a
join tableb b on a.opcode = b.opcode
group by a.name, a.opcode;
Oracle 使用 listagg
:
select a.name,
a.opcode,
listagg(month,', ') within group (order by b.pk) as months
from tablea a
join tableb b on a.opcode = b.opcode
group by a.name, a.opcode;
SQL Server 使用 for xml path
和 stuff
:
select a.*,
stuff((
select ', ' + month from tableb b
where a.opcode = b.opcode
order by pk
for xml path(''), type
).value('(./text())[1]', 'varchar(max)')
, 1, 2, '') as months
from tablea a;
关于mysql - 如何在查询后合并字段,我知道 CONCAT,但不是这种方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43170333/