1

我有兩個表格customersemails,emails表格可以爲每個公司有0個,1個或多個電子郵件。 現在,當我嘗試查詢使用無法從0:m關係表中的左連接中獲取記錄

select customers.*, emails.email 
from 
    customers left join 
    emails on emails.record_id = customers.id 

我無法讓所有的結果讓所有的客戶,而不是它只是返回那些有一個電子郵件或多個記錄。

任何幫助,將不勝感激。

+0

ddl爲兩個表 – amdixon

+2

您確定結果不正確?您正在使用左連接且對客戶沒有任何限制,因此無法將客戶表中的記錄退回。 –

+0

我無法上傳圖片,否則我會爲此發佈ERD,有沒有辦法這麼做... @ jedrzej.kurylo –

回答

1

問題可能出在您的WHERE子句中,它正在篩選您的左(客戶)表記錄。另外,如果您在Email表中有多個記錄與一個CustomerID相對應,那麼您必須使用distinct。

SELECT Distinct customers.*, emails.email from customers LEFT JOIN emails on 
customers.id = emails.record_id WHERE (user_type = 'customer' OR user_type is 
null) and (type='Call Centre' or type is null) 
相關問題