首先mysql方法,原生写法,这边以mysql8.0为例,user表有个lat_login字段,查询当天的数据
select * from users where DATE(last_login) = CURDATE();
再就是sqlserver的写法,同样的表,字段
select * from confirmation_documents where DATEDIFF(day,last_time,GETDATE())=0
如果要查询昨天的数据,将0改成1。
sqlserver里面的DATEDIFF()函数及GetDate()函数,具体方法如下:
DATEDIFF ( datepart , startdate , enddate)
释义:计算时间差
datepare值:year | quarter | month | week | day | hour | minute | second | millisecond
startdate:开始日期
enddate :结束日期
GetDate()
释义:获取当前的系统日期
下面例子中表名为tablename,条件字段名为inputdate
查询今天
SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0
查询昨天
SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1
查询本周
SELECT * FROM tablename where datediff(week,inputdate,getdate())=0
查询上周
SELECT * FROM tablename where datediff(week,inputdate,getdate())=1
查询本月
SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0
查询上月
SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1
查询本季度的
select * from T_InterViewInfo where datediff(QQ,inputdate,getdate())=0