我正在尝试显示3个表中的所有行。
我需要显示完整的客户列表。 每个客户可以提交0个,1个或更多。 每个提交可以有0,1或更多的产品。
CustomerID | submissionID | submissionProductID
001 | s001 | sp001
001 | s002 | sp002
002 | s003 | sp003
002 | s003 | sp004
002 | s003 | sp005
003 | --- | ---
004 | s005 | ---
这样:
customer 1 has 2 submissions, 1 product in each.
customer 2 has 1 submission containing 3 products.
customer 3 has no submission.
customer 4 has 1 submission but no pruducts.
我似乎无法显示所有记录。 下面的SQL将显示所有客户,但不会显示多个submisison行或多个产品行。
当然,有些客户会提交空白产品。 一些客户提交的产品没有产品。
SELECT c.CustomerID as 'CustID', s.submissionID, sp.submissionProductID
FROM tblCustomer c
LEFT JOIN tblSubmission s on c.CustomerID = s.customerID
LEFT JOIN tblSubmissionProducts sp on s.submissionID = sp.submissionID
GROUP BY c.CustomerID
ORDER BY c.CustomerID;
任何帮助将不胜感激。 感谢。
答案 0 :(得分:3)
如果您只想获取所有记录,请尝试此操作;)
SELECT c.CustomerID as 'CustID', s.submissionID, sp.submissionProductID
FROM tblCustomer c
LEFT JOIN tblSubmission s on c.CustomerID = s.customerID
LEFT JOIN tblSubmissionProducts sp on s.submissionID = sp.submissionID
ORDER BY c.CustomerID;