mysql统计某段日期中某段时间的数据

中间的动态拼接是为了防止隔天查询(如17:30-8:30),当然就收数据后要进行处理让后在传入

SELECT count(DISTINCT barcode) AS count,DATE_FORMAT(date, '%Y-%m-%d') AS ti
FROM ag_cpparts
WHERE
DATE_FORMAT(date, '%Y-%m-%d') BETWEEN #{dateOne} and #{dateTwo}
and
<if test="date3 != null and date4!=null" >
DATE_FORMAT(date, '%T') BETWEEN #{date1} and #{date3} or
DATE_FORMAT(date, '%T') BETWEEN #{date4} and #{date2}
</if>
<if test="date3 == null and date4==null">
DATE_FORMAT(date, '%T') BETWEEN #{date1} and #{date2}
</if>
GROUP BY ti
ORDER BY date
limit ${page},${pageDataNumber}

需要什么格式可以自己设置:

   %a     缩写星期名
        %b     缩写月名
        %c     月,数值
        %D     带有英文前缀的月中的天
        %d     月的天,数值(00-31)
        %e     月的天,数值(0-31)
        %f     微秒
        %H     小时 (00-23)
        %h     小时 (01-12)
        %I     小时 (01-12)
        %i     分钟,数值(00-59)
        %j     年的天 (001-366)
        %k     小时 (0-23)
        %l     小时 (1-12)
        %M     月名
        %m     月,数值(00-12)
        %p     AM 或 PM
        %r     时间,12-小时(hh:mm:ss AM 或 PM)
        %S     秒(00-59)
        %s     秒(00-59)
        %T     时间, 24-小时 (hh:mm:ss)
        %U     周 (00-53) 星期日是一周的第一天
        %u     周 (00-53) 星期一是一周的第一天
        %V     周 (01-53) 星期日是一周的第一天,与 %X 使用
        %v     周 (01-53) 星期一是一周的第一天,与 %x 使用
        %W     星期名
        %w     周的天 (0=星期日, 6=星期六)
        %X     年,其中的星期日是周的第一天,4 位,与 %V 使用
        %x     年,其中的星期一是周的第一天,4 位,与 %v 使用
        %Y     年,4 位
        %y     年,2 位

原文地址:https://www.cnblogs.com/cw828/p/10063607.html