简体   繁体   中英

MySQL Query for count of multiple values

I'm trying to retrieve the count of multiple values but can get only partial count.

My code

SELECT COUNT(*) as count, `dateadded` FROM s2 WHERE 'LEVEL` IN (1,2,3) and 
client = 'myuser' GROUP BY `LEVEL`,`dateadded` ORDER BY `dateadded` DESC 
LIMIT 1");

My table

client   dateadded     level
myuser   2019-01-21      3
myuser   2019-01-21      2
myuser   2019-01-21      5
myuser   2019-02-16      3
myuser   2019-02-16      2
myuser   2019-02-16      8
myuser   2019-02-16      2

My return value should be: 3 -> latest date (2019-02-19) and count of 1,2 and 3.

I want to count how many 1, 2 and 3 from the latest date only.

Thank you very much!!

Nathalie

While your requirement is not clear, I assume you wanted the count of LEVEL, dateadded for the latest date alone.

This should help you:

SELECT COUNT(*) as count, `dateadded` , `LEVEL` FROM s2 WHERE `LEVEL` IN 
(1,2,3) and 
client = 'myuser' GROUP BY `dateadded`,`LEVEL` ORDER BY `dateadded` DESC 
LIMIT 1;

If you don't want to count by each level, then use this:

SELECT COUNT(*) as count, `dateadded` FROM s2 WHERE `LEVEL` IN 
(1,2,3) and 
client = 'myuser' GROUP BY `dateadded` ORDER BY `dateadded` DESC 
LIMIT 1;

Try this:

SELECT COUNT(*) as count, `dateadded` FROM s2 WHERE 'LEVEL' IN (1,2,3) and 
client = 'myuser' and `dateadded` IN (select max(dateadded) from s2) GROUP BY `LEVEL`,`dateadded` ORDER BY `dateadded` DESC 
LIMIT 1");

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