簡體   English   中英

左聯接不會返回所有行

[英]left join does not return all rows

我有兩個表, property_mastertanant_master ,字段如下:

property_master-> p_id,pname

tanant_master-> t_id,p_id,t_name;

我在property_master有3個屬性,例如PRO1,PRO2,PRO3,在tanant_master 2個租戶,例如T1,T2。

T1分配給PRO1,T2分配給PRO2,PRO3中沒有任何租戶,因此現在我想查找所有屬性中的租戶總數,因此我的查詢如下:

SELECT p.p_id AS code, t.p_name AS Property, count( p.t_id ) AS total_count
FROM tanant_master p
LEFT JOIN property_master t ON t.p_id = p.p_id
GROUP BY p.p_id

當我運行此查詢時,它將僅提供2個屬性值(如PRO1和PRO2),但我也希望將PRO3計數為0。如何解決此問題?

左聯接返回表左側的所有行。 在這種情況下,這是tanant_master表,因此您將獲得所有租戶。

要獲取所有屬性,請使用左側的表:

SELECT p_id AS code,
       p_name AS Property,
       COUNT(t_id) AS total_count
FROM property_master
LEFT JOIN tanant_master USING (p_id)
GROUP BY p_id

暫無
暫無

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

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