1、查询状态为0的数据,并且>1的
select uid,count(*) as count from fy_timing where `status`=0 group by uid having count>1 复制代码
在使用聚合函数之前,我们可以通过 where 对查询加限制条件,那么如果在 group by 之后我们要对分组里面的数据再加限制条件怎么办呢?答案是利用 having。
having 子句可以让我们筛选成组后的各组数据。
where 子句在聚合前先筛选记录,也就是说作用在 GROUP BY 子句和 HAVING 子句前。而 having 子句是在聚合后对组记录进行筛选。
having 子句出现的数据必须在 group by 之后,order by 之后。
对 uid 按照出现次数排序之后,我们还想只查出状态为0的数据,可以这样写:
select uid,count(*) as count from fy_timing where `status`=0 group by uid having count>1 order by `count` 复制代码
SELECT uid, count( * ) AS count FROM fy_timing WHERE `status` = 0 GROUP BY uid HAVING count > 1 ORDER BY `count`