我想连接两个表并将两行显示为一行



我想要这样的东西:

详情       信息

                   ABC 1,2,3

而不是:

详情       信息

                   ABC 1

                   ABC 2

                   ABC 3

您可以使用GroupConcat

参考如下:

https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

这是SQL服务器的解决方案

select Particulars, stuff((
select ',' + cast(Info as varchar(20))
from YourTable b
where a.Particulars = b.Particulars
for xml path('')), 1, 1, '') as Info
from YourTable a group by Particulars

>如果您使用的是SQL Server 2017,则可以使用STRING_AGG来获得预期的结果。

SELECT Particulars, STRING_AGG (Info, ',') AS Info
FROM TableName 
GROUP BY Particulars; 

最新更新