【问题标题】:how to self join a table to get more info [duplicate]如何自我加入表格以获取更多信息[重复]
【发布时间】:2012-09-16 03:33:35
【问题描述】:

可能重复:
Concatenate many rows into a single text string?
How to use GROUP BY to concatenate strings in SQL Server?

我有一个表客户(id、person_id、帐号),id 是主键。例如

id   person_id    account number
1    0001         acc00011
2    0001         acc00012
3    0002         acc00021

现在我想编写一个查询来自加入表以获取每个人的所有帐号。结果表应该是

person_id    account
0001         acc00011, acc00012
0002         acc00021

如何编写查询?

【问题讨论】:

    标签: sql-server self-join


    【解决方案1】:

    使用correlated subqueryFOR XML PATHGROUP BY 进行连接。使用 STUFF 从结果中删除前面的逗号。

    在此处查看示例:http://sqlfiddle.com/#!3/e5bd0/5

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我尝试了第一个链接的答案,它有效。谢谢。
      猜你喜欢
      • 2015-01-26
      • 2014-05-15
      • 2020-10-10
      • 2019-08-11
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2018-06-29
      • 2023-03-26
      相关资源
      最近更新 更多