简体   繁体   中英

MySQL: get the total query number after grouping and counting

I have a below query:

select count(*) c , id 
from hr 
where  time >= '2011-11-8 00:00:00' and 
       time <= '2011-12-9 23:59:59' 
group by id 
having c>3;

The result list could be very huge (up to 10,000 items sometime), I don't want to list all since I only want to get the total number. Apart from "select found_rows()", I'm trying to find one sql sentence to get the job done without printing the list. (I'm not using Perl, PHP or any other API, just sql)

Any idea?

Thanks in advance.

Yang

Wrap it in a subquery and count the records again

SELECT COUNT(*) totalCount
FROM
(
    select count(*) c , id 
    from hr 
    where  time >= '2011-11-8 00:00:00' and 
           time <= '2011-12-9 23:59:59' 
    group by id 
    having COUNT(*) > 3
) x
SELECT COUNT(*) FROM (SELECT id, COUNT(*) c FROM hr WHERE time >= '2011-11-8 00:00:00' AND time <= '2011-12-9 23:59:59' GROUP BY id) counts WHERE counts.c > 3;

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