我有三個表:如何獲得左連接或完全連接?
Customers表:
+------------------+------------+------------+
| Customer | Start Date | End Date |
+------------------+------------+------------+
| Cantrell, Beck | 02/13/2016 | 12/22/2016 |
| Frazier, Urielle | 11/13/2015 | 01/03/2017 |
| Frost, Kato | 05/27/2016 | 09/17/2016 |
| Larsen, Kuame | 10/27/2015 | 08/09/2016 |
| Jennings, Echo | 12/14/2015 | 03/09/2017 |
+------------------+------------+------------+
付款表:
+------------------+------------+------------+
| Customer | Date | Amount |
+------------------+------------+------------+
| Frost, Kato | 05/27/2016 | 180.00 |
| Frost, Kato | 06/06/2016 | 20.00 |
| Frost, Kato | 06/07/2016 | 40.00 |
| Frost, Kato | 06/13/2016 | 100.00 |
| Frost, Kato | 06/20/2016 | 40.00 |
| Frost, Kato | 06/27/2016 | 80.00 |
| Frost, Kato | 07/05/2016 | 60.00 |
| Frost, Kato | 07/12/2016 | 40.00 |
+------------------+------------+------------+
我也有第三個表,日期,這僅僅是從1開始日期列表/ 1/2010至1/1/2020。
客戶在每週的開始日期當天到期。爲了得到每個客戶收費的項目的全部名單,我創建了以下查詢:
SELECT Dates.Date,
IIf([Dates].[date]=[Customers].[Start Date],177,77) AS Accrued
FROM Customers, Dates
WHERE (((Customers.[Start Date])<=[date])
And ((Customers.[End Date])>=[date])
AND ((Weekday(Dates.date))=Weekday(Customers.[Start Date]))
And ((Customers.Customer)=[Which Customer?]));
產生這樣的結果:
+--------------------+--------------------+
| Date | Accrued |
+--------------------+--------------------+
| 5/27/2016 | 177 |
| 6/3/2016 | 77 |
| 6/10/2016 | 77 |
| 6/17/2016 | 77 |
| 6/24/2016 | 77 |
| 7/1/2016 | 77 |
| 7/8/2016 | 77 |
| 7/15/2016 | 77 |
| 7/22/2016 | 77 |
| 7/29/2016 | 77 |
| 8/5/2016 | 77 |
| 8/12/2016 | 77 |
| 8/19/2016 | 77 |
| 8/26/2016 | 77 |
| 9/2/2016 | 77 |
| 9/9/2016 | 77 |
| 9/16/2016 | 77 |
+--------------------+--------------------+
現在,我需要在所有的添加自己目前的付款,所以我添加了付款表。
[由客戶收費]查詢:
SELECT Dates.Date,
IIf([Dates].[date]=[Customers].[Start Date],177,77) AS Accrued,
Payments.Date,
Payments.Amount
FROM Dates,
Customers INNER JOIN Payments ON Customers.Customer = Payments.Customer
WHERE (((Customers.[Start Date])<=[Dates].[date])
AND ((Customers.[End Date])>=[Dates].[date])
AND ((Weekday([Dates].[date]))=Weekday([Customers].[Start Date]))
AND ((Customers.Customer)=[Which Customer?]));
這樣做的結果是每個計提重複每筆付款的列表。我想調整JOIN以查看是否可以僅將Accrued限制爲一次,但每次嘗試時,都會收到錯誤消息,指出存在模糊的外連接。
因此,我嘗試將它分成兩個查詢,然後將它們連接在一起。
[由客戶付款]查詢:
SELECT Payments.Date, Payments.Amount, Customers.Customer
FROM Customers LEFT JOIN Payments ON Customers.Customer = Payments.Customer
WHERE (((Customers.Customer)=[Which Customer?]));
其產生如下結果:
+--------------------+--------------------+
| Date | Amount |
+--------------------+--------------------+
| 05/27/2016 | 180.00 |
| 06/06/2016 | 20.00 |
| 06/07/2016 | 40.00 |
| 06/13/2016 | 100.00 |
| 06/20/2016 | 40.00 |
| 06/27/2016 | 80.00 |
| 07/05/2016 | 60.00 |
| 07/12/2016 | 40.00 |
+--------------------+--------------------+
我然後試圖做一個FULL OUTER JOIN,但看完後左右,它不看起來像Access支持他們。我找到了一些使用UNION來解決它的例子。
SELECT [Charges by Customer].Date, [Charges by Customer].Accrued, [Payments by Customer].Date, [Payments by Customer].Amount
FROM [Charges by Customer] LEFT JOIN [Payments by Customer] ON [Charges by Customer].Customer = [Payments by Customer].Customer
UNION
SELECT [Payments by Customer].Date, [Payments by Customer].Amount, [Payments by Customer].Date, [Payments by Customer].Amount
FROM [Payments by Customer] LEFT JOIN [Charges by Customer] ON [Payments by Customer].Customer = [Charges by Customer].Customer
經過一段時間的擺弄之後,我很困惑,不知道該從哪裏開始。我有點迷路了Access。我對MySQL更加熟悉,差異(和UI)讓我很難將自己的頭腦放在需要做的事情上來獲得我想要的結果。
我理想中的結果會是這樣的:
+-----------+---------+------------+--------+
| Date | Accrued | Date | Amount |
+-----------+---------+------------+--------+
| 5/27/2016 | 177 | 05/27/2016 | 180.00 |
| 6/3/2016 | 77 | | |
| | | 06/06/2016 | 20.00 |
| | | 06/07/2016 | 40.00 |
| 6/10/2016 | 77 | | |
| | | 06/13/2016 | 100.00 |
| 6/17/2016 | 77 | | |
| | | 06/20/2016 | 40.00 |
| 6/24/2016 | 77 | | |
| | | 06/27/2016 | 80.00 |
| 7/1/2016 | 77 | | |
| | | 07/05/2016 | 60.00 |
| 7/8/2016 | 77 | | |
| | | 07/12/2016 | 40.00 |
| 7/15/2016 | 77 | | |
| 7/22/2016 | 77 | | |
| 7/29/2016 | 77 | | |
| 8/5/2016 | 77 | | |
| 8/12/2016 | 77 | | |
| 8/19/2016 | 77 | | |
| 8/26/2016 | 77 | | |
| 9/2/2016 | 77 | | |
| 9/9/2016 | 77 | | |
| 9/16/2016 | 77 | | |
+-----------+---------+------------+--------+
如果您有任何意見或解決方案,我會非常感激。謝謝!
對我的答案有什麼想法? – objectNotFound