gpt4 book ai didi

sql - 用sql连接表

转载 作者:行者123 更新时间:2023-12-04 06:22:45 24 4
gpt4 key购买 nike

我有两张 table 我想加入。一个包含客户,名为“客户”。另一个表“Customeraccounts”。这些表与客户 ID 相关。像这样。

Customers
1, john, Smith
CustomerAccounts
1, 1, 12345678
2, 1, 98765432

我如何将表加入输出将是:
1, john, smith, 12345678, 98765432

最佳答案

也许这对你有用:

SELECT Customers.Id, 
Customers.LastName,
Customers.FirstName,
STUFF((SELECT ',' + CustomerAccounts.Number
FROM CustomerAccounts
WHERE CustomerAccounts.CustomerId = Customers.Id
FOR XML PATH('')
),1,1,'')
AS AllCustomerAccounts
FROM Customers

关于sql - 用sql连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6369670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com