像这样的示例,有没有办法让select子查询在单个列中返回多个记录?
示例代码:
select u.fname,
u.lname,
(select item_id from products where user_id=u.user_id) as ITEM_IDS
from users u
答案 0 :(得分:2)
这有效。
select u.fname,
u.lname,
STUFF(
(SELECT ',' + CAST(item_id AS VARCHAR(20)) AS [text()]
FROM products AS P
where P.user_id=u.user_id
FOR XML PATH('')), 1, 1, NULL) AS ITEM_IDS
from users u