我尝试返回带有一个字段的查询集,该字段是django中的列表字符串,例如,如下所示:
Name:
Chicken Wing
Ids:
2529,2695,10829,10995,12129,12295 当我迁移到SQL Server2017数据库时,我只知道如何通过支持MySQL数据库的GROUP_CONCAT获取查询集。它不工作,我检查了SQL Server2017支持STRING_AGG,但我不知道如何在django中使用它。(我使用的是django rest框架)。
有人能帮我吗?
发布于 2019-11-26 15:17:45
您可以使用STUFF函数来获得预期的输出。
create table #temp(
id int,
name varchar(20)
)
insert into #temp
select 1, 'abc'
union
select 2, 'abc'
union
select 3, 'abc'
union
select 4, 'xyz'
union
select 5, 'xyz'
union
select 6, 'xyz'
SELECT distinct m.name as name,
STUFF((
SELECT ',' + cast(te.id as varchar(50))
FROM #temp te
WHERE t.name = te.name
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM #temp thttps://stackoverflow.com/questions/59044156
复制相似问题