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

时间:2023-01-04 15:30:11

I want something like:

我想要的东西:

Particulars         Info

详情资料

Abc                    1,2,3

Abc 1,2,3

instead of :

代替 :

Particulars         Info

详情资料

Abc                    1

Abc 1

Abc                    2

Abc 2

Abc                    3

Abc 3

3 个解决方案

#1


3  

you can use groupconcat

你可以使用groupconcat

Reference given below::

参考如下::

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

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

#2


1  

this is the solution for sql-server

这是sql-server的解决方案

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

#3


0  

If you are using SQL Server 2017, you can use STRING_AGG to get your expected result.

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

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

#1


3  

you can use groupconcat

你可以使用groupconcat

Reference given below::

参考如下::

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

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

#2


1  

this is the solution for sql-server

这是sql-server的解决方案

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

#3


0  

If you are using SQL Server 2017, you can use STRING_AGG to get your expected result.

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

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