1、ibatis根据时间查询 SELECT 字段 FROM WHERE 1=1 <dynamic> <isNotEmpty property="keyWord" prepend="and"> (p1.problem_code like concat('%',#keyWord#,'%') or p1.problem_title like concat('%',#keyWord#,'%')) </isNotEmpty> <isNotEmpty prepend="and" property="askTimeMin"> <![CDATA[DATE_FORMAT(ask_time, '%Y-%m-%d') >= ]]>DATE_FORMAT(#askTimeMin#, '%Y-%m-%d') </isNotEmpty> <isNotEmpty prepend="and" property="askTimeMax"> <![CDATA[DATE_FORMAT(ask_time, '%Y-%m-%d') <= ]]>DATE_FORMAT(#askTimeMax#, '%Y-%m-%d') </isNotEmpty> </dynamic> order by ask_time desc limit #startRow#,#pageSize# </select> 2、查询时间段 2.1、查询分钟相差半小时: SELECT *, TIMESTAMPDIFF(MINUTE, createtime, NOW()) FROM test WHERE TIMESTAMPDIFF(MINUTE, createtime, NOW()) > 30 TIMESTAMPDIFF(MINUTE, createtime, NOW()),用now()-createtime的时间相差数为分钟相差数。 2.2、<!-查相差的天数-> SELECT *, TIMESTAMPDIFF(DAY, createtime, NOW()) FROM test WHERE TIMESTAMPDIFF(DAY, createtime, NOW()) > 0.1 2.3、今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 2.4、昨天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=1 2.5、7天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7 2.6、30天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30 2.7、本月的所有数据:select * from 表名 where DateDiff(mm,datetime类型字段,getdate())=0 2.8、本年的所有数据:select * from 表名 where DateDiff(yy,datetime类型字段,getdate())=0 3、查询时间 3.1、查询今天是今年的第几天: select datepart(dayofyear,getDate()) 3.2查询今天是本月的第几天: 1. select datepart(dd, getDate()) 2.select day(getDate()) 3.3查询本周的星期一日期是多少 (注意:指定日期不能是周日,如果是周日会计算到下周一去。所以如果是周日要减一天) SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0) 3.4、查询昨天日期:select convert(char,dateadd(DD,-1,getdate()),111) //111是样式号,(100-114) 3.5、查询本月第一天日期:Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) as firstday 3.6查询本月最后一天日期:Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) as lastday //修改-3的值会有相应的变化 3.7本月有多少天: select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast((cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' ) as datetime )))) 3.8、求两个时间段相差几天:select datediff(day,'2012/8/1','2012/8/20') as daysum 3.9、在指定的日期上±N天:select convert(char,dateadd(dd,1,'2012/8/20'),111) as riqi //输出2012/8/21 3.10、在指定的日期上±N分钟:select dateadd(mi,-15,getdate()) //查询当前时间15分钟之前的日期