为什么这个查询不允许我做一个小组?

时间:2014-08-22 19:52:50

标签: mysql

我想通过ID向我展示最近提交的记录,但我只想在最近的这两个日期之间找到它。但是,它不允许我分组。

select id, type, location_id, max(submitted)
  from events 
 where type='status'
   and max(submitted) between '2014-08-05' and '2014-08-22'
 group by id

1 个答案:

答案 0 :(得分:4)

使用having

等聚合函数时,请使用max()子句
select id, type, location_id, max(submitted) 
from events 
where type='status'
group by id, type, location_id
having max(submitted) between '2014-08-05' and '2014-08-22'