【发布时间】:2017-07-27 20:16:46
【问题描述】:
我有 2 个表:Customer 和 SubAccount。Customer(customerId, customerName, address, city, state)SubAccount(subAccountID, subAccountName, customerID, subAddress, subCity, subState)
我想像这样选择父客户和他们的每个子帐户:
+------------+---------------+
| Customer | SubAccount |
+------------+---------------+
| Customer1 | null |
| Customer1 | SubAccount1 |
| Customer1 | SubAccount2 |
| Customer2 | null |
| Customer2 | SubAccount1 |
| Customer3 | null |
| Customer3 | SubAccount1 |
+------------+---------------+
不过,做一个简单的
SELECT Customer.CustomerName, SubAccount.subAccountName
FROM Customer
LEFT JOIN SubAccount ON SubAccount.CustomerId = Customer.CustomerID
不起作用。它只显示
+------------+---------------+
| Customer | SubAccount |
+------------+---------------+
| Customer1 | SubAccount1 |
| Customer1 | SubAccount2 |
| Customer2 | SubAccount1 |
| Customer3 | SubAccount1 |
+------------+---------------+
选择的正确方法是什么?
【问题讨论】: