首页 > 解决方案 > 检查一个字段中的所有记录是否具有相同的值?

问题描述

select count(*) from step where job_id =31 ;
select count(*) from step where job_id =31 and status =2;

这是两个查询。我想知道在单个查询中是否count1相等count2

标签: mysqlsql

解决方案


select count(*) as jobCount,
       sum(status = 2) as statusCount,
       count(*) - sum(status = 2) as countDifference,
       (count(*) - sum(status = 2)) = 0 as isEqual
from step 
where job_id = 31;

推荐阅读