我有三张桌子,
我想显示所有地点的列表,其中列出了对该地点发表评论的所有用户。
McDonalds Jill, Suzy, Bob
Walmart Fred, Joe, Suzy, Larry
Library Joe, Suzy
...
我正在尝试使用coalesce
函数来实现此功能,但我遇到了麻烦。我做错了什么?
SELECT places.name, COALESCE(users.name+",")
FROM comments
JOIN places
ON comments.place_id = places.id
WHERE user_id = users.id
GROUP BY places.name
感谢。
答案 0 :(得分:1)
试试这个!
使用Coalesce与此无关,请尝试将STUFF与XML PATH('')
一起使用SELECT t1.name,
STUFF(
(
SELECT ',' + [places.name] FROM comments
JOIN places
ON comments.place_id = places.id
WHERE user_id = users.id
GROUP BY places.name
for xml path('')
),1,1,'') as t1 from table <group by>