SQL資料庫查詢時間語句一天 一周 一月 一年

2021-10-09 14:45:08 字數 936 閱讀 9178

#求和的統計 列:

select sum(資料庫表字段) from 表名 where date_sub(curdate(),interval 4 day) <= date(時間字段)

#查詢最近幾天的時間資料

select * from 表名 where date_sub(curdate(),interval 4 day) <= date(時間字段)

#查詢昨天的資料

select * from 表名 where to_days(now()) - to_days(時間字段) <= 1

#查詢一周時間的資料

select * from 表名 where 時間字段 between current_date() -7 and sysdate()

#查詢最近乙個月的資料

select * from 表名 where date_sub(curdate(),interval 1 month) <= date(時間字段)

#查詢本月的資料

select * from 表名 where date_format(時間字段,'%y%m') = date_format(curdate(),'%y%m')

#查詢最近一年的資料

select * from 表名 where date_sub(curdate(),interval 1 year) <= date(時間字段)

#查詢某一年的資料

select * from 表名 where date_format(時間字段,'%y') = '2020'

#查詢某一年某一月的資料

select * from 表名 where date_format(時間字段,'%y-%m') = '2020-09'

#查詢某一年某一月某一天的資料

select * from 表名 where (datediff(時間字段,'2020-09-19') = 0)

sql查詢時間段之間的資料(一天 一周等)

to days now to days change time 1 昨天的資料 change time 為時間字段 to days now to days check in 0 今天的資料 check in 為時間字段 date sub curdate interval 30 day date ch...

資料庫查詢時間操作

oracle根據時間過濾 select from bzcarticle where content like 小 and addtimeaddtime是表裡的字段,字段型別是date,要查詢在2018年7月20號之前的資料。這條語句是根據內容和時間兩個條件過濾 mysql根據時間過濾。basic d...

SQL一天一練(二)

select prod id,prod name from products where prod name like fish 這條sql語句是找出所有以fish開頭的prod name 那麼如果這樣寫 select prod id,prod name from products where pr...