1. 程式人生 > >mysql統計某段日期中某段時間的資料

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}