gpt4 book ai didi

tsql - T SQL - 多次连接列

转载 作者:行者123 更新时间:2023-12-04 06:27:35 26 4
gpt4 key购买 nike

所以我有下表:
ID | Product_Image300 | /300-01.jpg300 | /300-02.jpg301 | /301.jpg302 | /302.jpg
每个 ID 可以有无限数量的图像。我需要将所有图像引用连接到一列中,但在生成以下输出时遇到问题:
ID | Product Images300 | /300-01.jpg; /300-02.jpg;301 | /301.jpg;

最佳答案

-- cte with test data
;with T (ID, Product_Image) as
(
select 300, '/300-01.jpg' union all
select 300, '/300-02.jpg' union all
select 301, '/301.jpg' union all
select 302, '/302.jpg'
)

select
T.ID,
(select T2.Product_Image+'; '
from T as T2
where T.ID = T2.ID
for xml path(''), type).value('.[1]', 'nvarchar(max)') as Product_Images
from T
group by T.ID

结果:
ID   Product_Images
---- -------------------------
300 /300-01.jpg; /300-02.jpg;
301 /301.jpg;
302 /302.jpg;

关于tsql - T SQL - 多次连接列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857975/

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