场景

某业务记录表中记录的所有员工的某记录。

要实现统计截止到现在为止的符合某些条件的人数。实现效果类似如下

MyBatis+Mysql实现从记录表中统计符合条件的人数_.net

 

 

实现

比如查询本月的符合条件的人数



<select id="selectCurrentMonthNum" resultType="Integer">
SELECT count(*) from
(
select gh from dp_ddjl
where sh =1 and sfcl =1 and htsfcl =1
and date_format(ddsj,'%y%m') = date_format(#{currnetMonth},'%y%m')
GROUP BY gh
) a
</select>


切记外层查询时要给设置别名。

其中gh是工号是作为人员分类的标志。

然后currentmonth是传递的当前时间,截取月份就行。

同理如果是要统计截止到今天之前的符合条件的人数



<select id="selectHtDqNum"  resultType="Integer">
SELECT
count( * )
FROM
(
SELECT
gh
FROM
ht_htcx
WHERE
isdelete = 0
AND date_format(#{today}, '%y%m%d' ) >date_format( jssj, '%y%m%d' )
GROUP BY
gh
) a
</select>