左连接没有结果

时间:2012-12-19 05:52:36

标签: sql sql-server join left-join

这是两张桌子。

tbl_Invoice

Invoice

tbl_Payment

Payment

我想要的是Pending Invoices特定客户。

现有查询如下:

Select * from tbl_Invoice I 
    left join tbl_payment P on I.client_id = P.client_id
    left join tbl_client C on I.client_id = C.client_id
    where I.invoice_Id not in (P.invoice_Id) 
    and I.client_id = 8

但它给出了空白结果,因为付款表是空白的。

1 个答案:

答案 0 :(得分:8)

您需要将条件从WHERE移至ON

Select * from tbl_Invoice I 
left join tbl_payment P on (I.client_id = P.client_id 
 AND p.invoice_id <> I.invoice_Id)
left join tbl_client C on I.client_id = C.client_id

WHERE I.client_id = 5