MySQL查询多个值的计数

时间:2019-02-21 07:04:49

标签: mysql

我正在尝试检索多个值的计数,但只能获取部分计数。

我的代码

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");

我的桌子

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

我的返回值应该是:3->最近日期(2019-02-19)并计数1,2和3。

我只想计算最近一个日期的1个,2个和3个。

非常感谢您!

Nathalie

2 个答案:

答案 0 :(得分:1)

虽然您的要求不清楚,但我假设您只需要LEVEL的计数(仅针对最新日期加上日期)。

这应该对您有帮助

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;

如果您不想按每个级别进行计数,请使用以下方法:

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;

答案 1 :(得分:0)

尝试一下:

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");