SQL 多对多 JOIN
SQL many-to-many JOIN
我很难加入两个 table。
我有 tables
Customer_table
---------------------------------------
| CustomerId(PK) | Firstname | Lastname |
---------------------------------------
CustomerInterest_table
----------------------------------------
| CustomerId(PK,FK) | InterestId(PK,FK) |
----------------------------------------
Interest_table
-------------------------------
| InterestId(PK) | InterestInfo |
-------------------------------
我想做的是select每一位顾客,在table.
上加入FK参考的利益
最终我想获取一个结果,其中包含从客户 table 获取的客户以及从 CustomerInterest_table 获取的客户兴趣。
我喜欢像这样构建对象
{
customerId : 'Id12345,
firstname : 'John',
lastname : 'Doe',
interests : [{interestId : 1, interestInfo : 'Apples'}]
}
我将如何获取和加入 tables?
非常感谢任何帮助。
数据库设计(First Normal Form)假设该列应该是简单类型,在你的情况下它意味着无数组。相反,您可以从多个选定的行中获取所需的内容:
SELECT customerId, firstname, lastname, interestId, InterestInfo
FROM Customer_table c
INNER JOIN CustomerInterest_table tc
ON c.customerId = tc.customerId
INNER JOIN Interest_table i
ON tc.InterestId = i.InterestId
ORDER BY customerId
最后一个 ORDER BY
允许您强制按行顺序排列,这样同一客户的兴趣将一一显示。
或者,如果客户可能没有兴趣,您可以利用 LEFT JOIN
(然后两列 interestId、InterestInfo 将为 NULL)
SELECT customerId, firstname, lastname, interestId, InterestInfo
FROM Customer_table c
LEFT OUTER JOIN CustomerInterest_table tc
ON c.customerId = tc.customerId
INNER JOIN Interest_table i
ON tc.InterestId = i.InterestId
ORDER BY customerId
更新
或者(如果你真的想不惜一切代价将所有内容都放在单列中)你可以将结果转换为 XML 数据类型,然后最后一列将组成复杂的 XML:
SELECT customerId, firstname, lastname
, [Name]
, (STUFF((SELECT CAST(', ' + interestId AS VARCHAR(MAX) + ':' + InterestInfo)
FROM Interest_table i
WHERE tc.InterestId = i.InterestId
FOR XML PATH ('')), 1, 2, '')) AS interests
FROM Customer_table c
INNER JOIN CustomerInterest_table tc
ON c.customerId = tc.customerId
(p.s。抱歉语法没有检查正确性)
我很难加入两个 table。
我有 tables
Customer_table
---------------------------------------
| CustomerId(PK) | Firstname | Lastname |
---------------------------------------
CustomerInterest_table
----------------------------------------
| CustomerId(PK,FK) | InterestId(PK,FK) |
----------------------------------------
Interest_table
-------------------------------
| InterestId(PK) | InterestInfo |
-------------------------------
我想做的是select每一位顾客,在table.
上加入FK参考的利益最终我想获取一个结果,其中包含从客户 table 获取的客户以及从 CustomerInterest_table 获取的客户兴趣。
我喜欢像这样构建对象
{
customerId : 'Id12345,
firstname : 'John',
lastname : 'Doe',
interests : [{interestId : 1, interestInfo : 'Apples'}]
}
我将如何获取和加入 tables? 非常感谢任何帮助。
数据库设计(First Normal Form)假设该列应该是简单类型,在你的情况下它意味着无数组。相反,您可以从多个选定的行中获取所需的内容:
SELECT customerId, firstname, lastname, interestId, InterestInfo
FROM Customer_table c
INNER JOIN CustomerInterest_table tc
ON c.customerId = tc.customerId
INNER JOIN Interest_table i
ON tc.InterestId = i.InterestId
ORDER BY customerId
最后一个 ORDER BY
允许您强制按行顺序排列,这样同一客户的兴趣将一一显示。
或者,如果客户可能没有兴趣,您可以利用 LEFT JOIN
(然后两列 interestId、InterestInfo 将为 NULL)
SELECT customerId, firstname, lastname, interestId, InterestInfo
FROM Customer_table c
LEFT OUTER JOIN CustomerInterest_table tc
ON c.customerId = tc.customerId
INNER JOIN Interest_table i
ON tc.InterestId = i.InterestId
ORDER BY customerId
更新
或者(如果你真的想不惜一切代价将所有内容都放在单列中)你可以将结果转换为 XML 数据类型,然后最后一列将组成复杂的 XML:
SELECT customerId, firstname, lastname
, [Name]
, (STUFF((SELECT CAST(', ' + interestId AS VARCHAR(MAX) + ':' + InterestInfo)
FROM Interest_table i
WHERE tc.InterestId = i.InterestId
FOR XML PATH ('')), 1, 2, '')) AS interests
FROM Customer_table c
INNER JOIN CustomerInterest_table tc
ON c.customerId = tc.customerId
(p.s。抱歉语法没有检查正确性)