sql查詢今天,昨天,七天,三十天內的資料
今天有個需求需要從查詢當天所有的資料,就去網上搜了一下,並把其他幾個通過時間條件查詢也記錄下來
1.查詢今天所有的資料
select * from customer_loan where DateDiff(dd,datetime型別欄位,getdate())=0
2.查詢昨天的所有資料
select * from customer_loan where DateDiff(dd,datetime型別欄位,getdate())=1
3.查詢7天內所有的資料
select * from customer_loan where DateDiff(dd,datetime型別欄位,getdate())<=7
4.查詢30天內所有資料
select * from customer_loan where DateDiff(dd.datetime型別欄位,getdate())<=30
5.本月所有的資料
select * from customer_loan where DateDiff(mm.datetime型別欄位,getdate())=0
6.查詢本年的所有資料
select * from customer_loan where DateDiff(yy,datetime型別欄位.getdate())=0