MySQL查詢某個時間段內所有的記錄總數

2021-08-04 07:33:18 字數 973 閱讀 5359

因為需要完成如下圖的效果需要查詢出最近乙個時間段內所有的記錄總數

有兩種方法hour()和date_format()。

hour()使用時如果計算最近一天的記錄需要新增過濾條件,而date_format()不需要新增過濾條件預設查詢最近一天的記錄

例子:

select

thermometer_return as '是否冷鏈',

count(id) as '數量',

hour (create_date) as '時間'

from

order_sale

where

company_id = '2'

and del_flag = '0'

and create_date >= '2017-07-25 00:00:00.0'

and create_date <= '2017-07-25 12:00:00.0'

group by

hour (create_date),

thermometer_return

order by

create_date

select date_format(create_date, '%h') as '時間', count(*) as '記錄總數',thermometer_return as '是否冷鏈' from order_sale

where

company_id = '2'

and del_flag = '0'

-- and create_date >= '2017-07-25 00:00:00.0'

-- and create_date <= '2017-07-25 12:00:00.0'

group by date_format(create_date, '%h'),thermometer_return;

mysql 查詢某個時間段的資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天 包括昨天和今天的資料 select from 表名 where to days now to days 時間欄位名 1 昨天 只包括昨天 select from 表名 where datedif...

mysql查詢時間段內資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 近7天select from 表名 where date sub curdate interval...

mysql查詢時間段內資料

今天 select from 表名稱 where to days 建立時間 字段 to days now 昨天 select from 表名稱 where to days now to days 建立時間 字段 1 近七天 select from 表名稱 where date sub curdate...