簡體   English   中英

左聯接不起作用-它忽略左表的記錄

[英]left join doesn't work - its ignoring the records of the left table

我有3張桌子

表客戶

ID NUMBER NAME
--------------------------------------
1  12345  Apple
2  23456  Orange
3  25896  Banana

表帳戶

 ID    CUST_NUMBR  TYPE    BILLING_FK
 -------------------------------------
 1     12345        B      9876
 2     23456        R      8765
 3     25896        R      7654

表計費

 ID    Start_Date  End_Date 
 -------------------------------------
 1     BLAH       BLAH       
 2     BLAH       BLAH             
 3     BLAH       BLAH       

關系

  CUSTOMER.NUMBER = ACCOUNT.CUST_NUMBR
  ACCOUNT.BILLING_FK  = BILLING.ID

有時,找不到客戶的帳戶,在這種情況下,開始日期必須為空。

如果我嘗試左加入,如果沒有帳戶,則看不到客戶。

 select name,number,start_date
 from customer left join account on customer.number = account.cust_number,
 billing
 where account.billing_fk = billing.id

如果沒有該客戶的帳戶,如何將開始日期設為空。

您的條件為account.billing_fk = billing.id將您的LEFT JOIN變成INNER JOIN 幾乎消除了所有NULL條目。

select name,number,start_date
from customer 
left join account on customer.number = account.cust_number
left join billing on account.billing_fk = billing.id

由於您的where子句中有以下語句,因此無法使用:

account.billing_fk = billing.id

嘗試將其移到left join section

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM