这将选择至少有一个已删除类字段的所有用户,并将显示所有类字段的数量和所有已删除的类字段的数量:
SELECT
userid,
count(*) as numberofClassifieds,
sum(classStatus=6) as numberofDeletedClassfields
FROM
your_table
GROUP BY userid
HAVING numberofDeletedClassfields>0
ORDER BY numberofDeletedClassfields DESC
编辑:这是一种使用连接的替代方法,它看起来不像其他查询那么优雅,但它确实更快:
SELECT dc.userid, c.numberofClassfields, dc.numberofDeletedClassfields
FROM
(select userid, count(*) as numberofDeletedClassfields
from your_table
where classStatus = 6
group by userid
having count(*)>0) dc
INNER JOIN
(select userid, count(*) as numberofClassfields
from your_table
group by userid) c
on dc.userid=c.userid
ORDER BY dc.numberofDeletedClassfields desc
EDIT2:无论如何,如果您正在寻找已删除类字段的最大数量,那么您可以使用它:
select max(numberofDeletedClassfields)
from (
select count(*) as numberofDeletedClassfields
from your_table
where classStatus = 6
group by userid) s