日期查詢是相對常見的查詢了,一般查詢都是查詢時間段內的資料(某天、某小時、幾號~幾號 等等),即使指定了時分秒如「2016-5-4 15:14:53」這麼乙個看上去是時間點的資料,仍然也是時間段,它是指「2016-5-4 15:14:53 000」到「2016-5-4 15:14:53 999」這1秒時間段內的資料。
日期查詢有下面的方面和注意點:
1. 查詢5月1日到5月4日這4天的所有記錄,使用》=和<=進行
select t.id,t.name
from t_tabel t
where 1=1
and t.date_field >= to_date('2016-05-01', 'yyyy-mm-dd')
and t.date_field <= to_date('2016-05-04', 'yyyy-mm-dd')
2. 查詢5月1日到5月4日這4天的所有記錄,使用between ……and進行select t.id,t.name
from t_tabel t
where 1=1
and t3.repay_paydate between to_date('2016-05-01', 'yyyy-mm-dd') and to_date('2016-05-04', 'yyyy-mm-dd')
3. 查詢5月1日這1天的所有資料,使用
去尾函式trunc()函式
select id,query_date
from t_table
where 1=1
and trunc(query_date) = trunc(to_date('2016-05-04','yyyy-mm-dd'))
4. 查詢5月1日這1天的所有資料,不使用trunc()函式
select id,query_date
from t_table
where 1=1
and l.query_date <= to_date('2016-05-04 23:59:59','yyyy-mm-dd hh24:mi:ss')
and l.query_date >= to_date('2016-05-04 00:00:00','yyyy-mm-dd hh24:mi:ss')
特註:示例3與示例4的區別在於:
如果使用去尾函式trunc(),則日期格式為:'yyyy-mm-dd' 。而不使用該函式時,日期格式應該為 :'yyyy-mm-dd hh24:mi:ss' 。
MYSQL根據日期查詢
風蕭蕭兮易水寒,壯士一去兮不復還 總結一下mysql的根據日期查詢資料和在日期區間查詢資料.首先,看資料表和表結構 建表語句 create tabledatetest idint 11 not null auto increment,brand namevarchar 100 default nul...
MySQL根據日期查詢
1 查詢當天的資料 select from 表名 where to days 時間字段 to days now 2 查詢當周的資料 select from 表名 where yearweek date format 時間字段,y m d yearweek now 3 查詢當月的資料 select f...
mybatis根據日期查詢 查詢日期並返回
1.查詢日期,返回日期字串 查詢所有部落格對應的年份,返回乙個集合 list string findgroupyear 2.根據日期查詢 根據年份查詢部落格資訊 list blog findbyyear param year string year findgroupyear resulttype ...