gpt4 book ai didi

sql-server-2005 - SQL 选择内容

转载 作者:行者123 更新时间:2023-12-02 21:30:56 25 4
gpt4 key购买 nike

我有project表。

这是我的查询,正在获取以下结果。

select top 5 proj_ID, Proj_NM 
from project

输出:

proj_ID Proj_NM  
-------------------
20 test1
21 test2
22 test3
24 test4
25 test5

我想得到这个输出。任何人都可以帮忙吗?

proj_ID Proj_NM  All_Proj_NM
---------------------------------
20 test1 test1,test2,test3,test4,test5
21 test2 test1,test2,test3,test4,test5
22 test3 test1,test2,test3,test4,test5
24 test4 test1,test2,test3,test4,test5
25 test5 test1,test2,test3,test4,test5

最佳答案

您可以使用 FOR XML PATH 来实现

select top 5 proj_ID, Proj_NM,
(select STUFF( (select top 5 ',' + Proj_NM
from project
order by proj_id
FOR XML PATH('')
), 1, 1, '')) AS All_Proj_NM
from project
order by proj_ID

关于sql-server-2005 - SQL 选择内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22292662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com