【问题标题】:MySQL query with left join, count and group with 3 tables带有 3 个表的左连接、计数和分组的 MySQL 查询
【发布时间】:2015-04-23 21:06:52
【问题描述】:

我有 3 个表,我试图在其中放置如下所示的连接查询。

下面是包含 3 个部分的部分表。

Section
*****************************
* section_id * section_name *
*****************************
*  1         * A            *
*  2         * B            *
*  3         * C            *
*****************************

下面是section_subject 表。第 1 部分包含 2 个主题,第 2 部分包含 2 个主题,第 3 部分包含 3 个主题。

Section_Subject
***********************************
* ss_id * section_id * subject_id *
***********************************
* 1     * 1          * 8          *
* 2     * 1          * 9          *
* 3     * 2          * 6          *
* 4     * 2          * 5          *
* 5     * 3          * 2          *
* 6     * 3          * 3          *
* 7     * 3          * 4          *
***********************************

下面是section_batch 表。仅第 3 部分包含 2 个批次

Section_Batch
*********************************
* sb_id * section_id * batch_id *
*********************************
* 1     * 3          * 6        *
* 2     * 3          * 7        *
*********************************

我希望查询产生以下结果

**************************************************************
* section_id * section_name * count_subjects * count_batches *
**************************************************************
* 1          * A            * 2              * 0             *
* 2          * B            * 2              * 0             *
* 3          * C            * 3              * 2             *
**************************************************************

我知道我们可以做一些子查询来达到上面的结果。但是如何使用左连接和组查询得到结果呢?

【问题讨论】:

    标签: mysql sql group-by left-join


    【解决方案1】:

    您需要分别对每个表执行left joingroup by 以获取计数,然后在它们之间进行连接

    SQL 小提琴:http://www.sqlfiddle.com/#!9/ea4ee/12

    select T1.section_id,
           T1.section_name,
           T1.subjects_count,
           T2.batch_count
    FROM (
    select S.section_id, 
           S.section_name,
           COUNT(SS.subject_id) as subjects_count
    from Section S
    LEFT JOIN Section_Subject SS
    on S.section_id = SS.section_id
    group by S.section_id, S.section_name )T1
    LEFT JOIN (
    select S.section_id, 
           S.section_name,
           COUNT(SB.batch_id ) as batch_count
    from Section S
    LEFT JOIN Section_Batch SB
    on S.section_id = SB.section_id
    group by S.section_id, S.section_name
      ) T2
     on T1.section_id = T2.section_id
    

    【讨论】:

    • 您的解决方案有效,但我不确定此查询的性能,因为它执行多个选择和分组依据。
    • @Malaiselvan 你可以测试一下。这种类型的解决方案通常更有效。特别是如果您有超过 2 个表连接到基表。想象一下有 6 个而不是 2 个。我敢打赌这种类型的 6 个单独的 group by 然后 6 个 join 而不是一个巨大的 7-table join 然后 group by。
    • 在姊妹网站 dba.se 的类似问题中查看我的答案:Help with this query
    【解决方案2】:

    我相信使用count(distinct) 可以满足您的需求。您必须使用distinct,因为在一个部分有多个主题和多个批次的情况下,连接会产生乘数效应。

    select
        s.section_id,
        min(t1.section_name) as section_name,
        count(distinct ss.subject_id) as subject_count,
        count(distinct sb.batch_id) as batch_count,
    from
        Section as s
        left join Section_Subject as ss on ss.section_id = s.section_id
        left join Section_Batch as sb on sb.section_id = s.section_id
    group by
        s.section_id
    

    顺便说一句,我认为左连接可能是内连接。

    【讨论】:

      【解决方案3】:

      您可以将countdistinct 一起使用:

      select t1.section_id
           , t1.section_name
           , count(distinct t2.subject_id) as count_subjects
           , count(distinct t3.batch_id) as count_batches
      from Section t1
      left join Section_Subject t2 on t1.section_id = t2.section_id
      left join Section_Batch t3 on t1.section_id = t3.section_id
      group by t1.section_id
             , t1.section_name
      

      SQLFiddle

      【讨论】:

      • @notulysees 第 3 部分的 count_subjects 和 count_batches 显示 6,这是错误的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 2013-06-02
      相关资源
      最近更新 更多