tsql 分组依据的聚合字符串
我有两个表:
名称(id,名称) 地址(id,name_id,地址)
我想编写返回我的查询: 名称,地址列表(地址1,地址2,地址3,..),
例如:
Select A.name, B.list_of_addresses
From Names A
Inner Join (Select name_id, /*list_of_addresses with comma between them*/
From Addresses
Group By name_id) B ON A.id=B.name_id
I have two tables:
Names(id, name)
Addresses(id, name_id, address)
I want to write query that return me:
name, address list (address1, address2, adress3, ..)
Something like:
Select A.name, B.list_of_addresses
From Names A
Inner Join (Select name_id, /*list_of_addresses with comma between them*/
From Addresses
Group By name_id) B ON A.id=B.name_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 SQL Server 2005 开始,您可以使用 For XML 作为实现这一目标的技巧。
如果您的地址中还没有逗号,那么它效果很好,但即使有,因为您的要求是在它们之间添加逗号..这可能同样“正确”。
You can use For XML as a trick to achieve that from SQL Server 2005 onwards.
It works well if you don't already have commas in the address, but even if it did, since your request is to put commas between them.. this is probably just as 'right'.