简体   繁体   中英

How to express count(distinct) with subquery in MySQL?

A query results a certain number. The query is:

select
count(distinct case when (A or B or C) and D then table_a.field1 else null end)
from table_a
    left join table_b on table_b.x = table_a.y
group by table_a.y
;

where A, B, C and D are given conditions. Now, written in this form:

select
sum((select count(1) from table_b where table_b.x = table_a.y and ((A or B or C) and D) ))
from table_a
    left join table_b on table_b.x = table_a.y
group by table_a.y
;

the result does not match the one we got with count(distinct).

What is the correct way of writing count(distinct) with a subquery?

It's not at all clear why you need a subquery. You still have the JOIN, so that subquery is potentially going to be "counting" the same rows multiple times.

If you want to get the number of distinct values for field1 in table_a which meets a set of criteria (on table_a), then you don't really need a subquery on table_b to get that. At least, I don't see anyway that you can get that result using a subquery on table_b.

Here's an example that returns an equivalent result:

 select (select sum(1) as mycount
           from ( select a.field1 
                    from table_a a
                    left join table_b on table_b.x = a.y
                   where a.y = t.y
                     and ( (A or B or C) and D )
                     and a.field1 IS NOT NULL
                   group by a.field1
                ) s
        ) as mycount
   from table_a t
  group by t.y

That's really the only way I know to get something equivalent to a COUNT(DISTINCT expr) . You've got to do a SELECT expr FROM ... WHERE expr IS NOT NULL GROUP BY expr , and then count the rows it returns. In this case, you could use either a COUNT(1) or a SUM(1).

(I'm not at all sure that answers the question you were asking, but it's my best shot at it.)

(We note that in your original query, you have a GROUP BY table_a.y , so that query can return multiple rows, each with its own count.

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