基于日期的MySQL计数(组)

时间:2019-02-20 14:00:15

标签: mysql

我试图计算仅针对最新日期(在这种情况下为2019-02-16)解决的问题数量

我的查询

SELECT COUNT(1) FROM table1 WHERE DIFFICULTY = 'High' ORDER BY dateadded DESC 
LIMIT 1

我的桌子

dateadded      Difficulty
2019-02-02        Medium
2019-02-09        Medium
2019-02-09        Low
2019-02-16        High
2019-02-16        High
2019-02-16        Medium

我的查询应该从最新日期-> 2019-02-16返回2 ...高+高

任何帮助将不胜感激。

Nathalie

2 个答案:

答案 0 :(得分:2)

您的查询应如下所示:

SELECT COUNT(*) as count, `dateadded` 
FROM `table1` 
WHERE `Difficulty` = 'High' 
GROUP BY `Difficulty`,`dateadded` 
ORDER BY `dateadded` DESC 
LIMIT 1

会返回:

2, 2019-02-16

答案 1 :(得分:0)

要计算添加的最后日期:

select max(dateadded) from table1

要计算在最后添加日期解决的问题数量:

select count(*)
from table1
where dateadded=(select max(dateadded) from table1) and difficulty='High'