1

基本上,我使用 LEFT JOIN 表来获取属于 PERSON 的 INVOICE 记录。

表格的快速概览以及相关记录。

table INVOICES    table GIGS        table BIDS               table PEOPLE
----------------  ----------------  -----------------------  ---------------- 
id | gig_id       id | bid_id       id | gig_id | person_id  id         
----------------  ----------------  -----------------------  ----------------
1  | 1            1  | 1             1 | 1      | 1          1
                  2  | 2             2 | 1      | 2          2

和我的加入查询...

SELECT invoices.* FROM invoices 
INNER JOIN gigs ON gigs.id = invoices.gig_id 
INNER JOIN bids ON bids.gig_id = gigs.id 
INNER JOIN people ON people.id = bids.person_id 
WHERE people.id = 2
GROUP BY invoices.id

和返回的结果...

INVOICE RESULT
--------------
id
--------------
1

事实是,people.id=2 没有任何发票,但上面的联接查询返回的结果就像它们一样。当一个人没有任何发票时,如何确保它返回空?

非常感谢您对此的任何提示!

4

1 回答 1

2

使用LEFT OUTER JOIN而不是INNER JOINPeople表开始(具有您想要查看所有数据的表)

像这样的东西:

SELECT 
    People.Id,
    invoices.* 
FROM 
    People    
LEFT OUTER JOIN     -- this here *might* be an INNER JOIN
    bids ON people.person_id = bids.person_id 
LEFT OUTER JOIN     -- this here *might* be an INNER JOIN
    gigs ON bids.gig_id = gigs.id 
LEFT OUTER JOIN
    invoices  ON gigs.id = invoices.gig_id 
GROUP BY 
    invoices.id
于 2012-04-16T14:02:07.903 回答