简体   繁体   中英

MySQL - count different fields on different conditions in one joined table

There is a hypothetical table called Table1, with the columns:

  • id
  • condition 1
  • condition 2
  • joinable_key

There is another one, Main_Table, whose ID corresponds with the joinable_key in the first one.

I would like to join them in such a fashion that I can count the rows of Table1 according to both condition 1 and condition 2 separately - that is, I would like to be able to perform "count(Table1.condition1) as first_condition, count(Table1.condtion2) as second_condition" on the select query.

The query would presumably look something like this:

SELECT  Main_Table.some_column, COUNT(Table1.condition1) AS first_condition, COUNT(Table1.condtion2) AS second_condition
FROM  Main_Table
LEFT JOIN Table1 AS T1 on (T1.joinable_key = Main_Table.id AND T1.condition1 = 'something')
LEFT JOIN Table1 AS T2 on (T2.joinable_key = Main_Table.id AND T2.condition2 = 'something else')
GROUP BY (Main_Table.id)

When this executes, however, both count results are equal, and actually multiply with each other. It is imperative that all results be included in the final output - including those that do not have any entries from Table1 - that is, if there is no row in Table1 with a joinable_key equal to Main_Table.id, it too needs to be included.

Before anyone suggest actually doing two separate queries and handling it through PHP - yes, I know why and how it can be done, but my goal is to find out whether or not this multi-count can be done all in one query.

Thank you

SELECT some_column,
       (SELECT COUNT(1)
          FROM Table1
         WHERE mt.id = joinable_key
           AND condition1 = 'something'
       ) AS first_condition,
       (SELECT COUNT(1)
          FROM Table1
         WHERE mt.id = joinable_key
           AND condition2 = 'something else'
       ) AS second_condition
  FROM MainTable mt
SELECT  Main_Table.some_column,
        SUM(t1.condition1 = 'something') AS first_condition,
        SUM(t1.condition2 = 'something_else') AS second_condition
FROM    Main_Table
LEFT JOIN
        Table1 AS t1
ON      t1.joinable_key = Main_Table.id
        AND
        (
        t1.condition1 = 'something'
        OR
        t1.condition2 = 'something else'
        )
GROUP BY
        Main_Table.id

If both condition1 and condition2 are selective, this syntax may be more efficient:

SELECT  Main_Table.some_column,
        (
        SELECT  COUNT(*)
        FROM    table1 t1
        WHERE   t1.joinable_key = main_table.id
                AND t1.condition1 = 'something'
        ),
        (
        SELECT  COUNT(*)
        FROM    table1 t1
        WHERE   t1.joinable_key = main_table.id
                AND t1.condition2 = 'something_else'
        )
FROM    Main_Table

You should create the following indexes:

table1 (id, condition1)
table1 (id, condition2)

for this to work fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM