2016-07-29 71 views
2

我有三個表:如何獲得左連接或完全連接?

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 |   |  | 
+-----------+---------+------------+--------+ 

如果您有任何意見或解決方案,我會非常感激。謝謝!

+0

對我的答案有什麼想法? – objectNotFound

回答

0

有幾個問題你的方法

  1. 查詢[由客戶收費]缺少列客戶

[由客戶收費]

SELECT Customers.Customer, 
     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)='Frost, Kato')) 
  • 您需要一個Right Join和Left Join的聯盟,然後按日期排序才能得到如下最終結果:
  • SELECT a.Customer, a.Date1, a.Accrued, a.Date2, a.Amount FROM ( select NZ(C.Customer,P.Customer) as Customer, NZ(C.Date, P.Date) as Date1, C.Accrued, P.Amount, P.Date as Date2 from [Charges by Customer] C RIGHT OUTER JOIN [Payments by Customer] P ON C.Customer = P.Customer and P.Date = C.Date UNION select C.Customer, C.Date, C.Accrued, P.Amount, P.Date as Date2 from
    [Charges by Customer] C LEFT OUTER JOIN [Payments by Customer] P ON C.Customer = P.Customer and P.Date = C.Date ) a Order by Date1

  • 還與聯盟您的查詢缺少的連接,通過日期(and P.Date = C.Date)加入條件。您只需加入「客戶」欄目[Charges by Customer].Customer and [Payments by Customer].Customer
  • 讓我知道您是否需要任何說明。

    +0

    感謝您的回覆。我現在正在度假,並沒有機會對它進行測試。我會繼續給你,讓他們不會過期。 – NicholasJohn16

    +0

    @ NicholasJohn16謝謝...讓我知道它是否真的有效...大聲笑 – objectNotFound

    +0

    工作很好。謝謝。 – NicholasJohn16