【问题标题】:left join does not return all rows左连接不返回所有行
【发布时间】:2015-01-20 12:06:13
【问题描述】:

我有两张表,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。我该如何解决这个问题?

【问题讨论】:

    标签: android sqlite left-join inner-join


    【解决方案1】:

    左连接返回其左侧表中的所有行。 在本例中,这是 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
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多