MySQL常用日期查詢 超全

2021-10-12 07:06:20 字數 4499 閱讀 5604

select timestamp(date_format(now(),'%y-%m-%d %h:%i:00'))
select timestamp(date_format(now(),'%y-%m-%d %h:00:00'))
select subdate(now(),interval 1 day)
select timestamp(curdate())
select timestamp(curdate(),'23:59:59')
select timestamp(subdate(curdate(),interval 1 day))
select timestamp(subdate(curdate(),interval 1 day),'23:59:59')
select timestamp(adddate(curdate(),interval 1 day))
select timestamp(adddate(curdate(),interval 1 day),'23:59:59')
select timestamp(subdate(curdate(),weekday(curdate())))
select timestamp(subdate(curdate(),weekday(curdate())),'23:59:59')
select timestamp(adddate(curdate(),6-weekday(curdate())))
select timestamp(adddate(curdate(),6-weekday(curdate())),'23:59:59')
select timestamp(subdate(curdate(), interval 1 week))
select timestamp(subdate(curdate(), interval 1 week),'23:59:59')
select timestamp(subdate(subdate(curdate(),weekday(curdate())),interval 1 week))
select timestamp(subdate(subdate(curdate(),weekday(curdate())),interval 1 week),'23:59:59')
select timestamp(subdate(adddate(curdate(),6-weekday(curdate())), interval 1 week))
select timestamp(subdate(adddate(curdate(),6-weekday(curdate())), interval 1 week),'23:59:59')
select timestamp(adddate(curdate(), interval 1 week))
select timestamp(adddate(curdate(), interval 1 week),'23:59:59')
select timestamp(adddate(subdate(curdate(),weekday(curdate())),interval 1 week))
select timestamp(adddate(subdate(curdate(),weekday(curdate())),interval 1 week),'23:59:59')
select timestamp(adddate(adddate(curdate(),6-weekday(curdate())), interval 1 week))
select timestamp(adddate(adddate(curdate(),6-weekday(curdate())), interval 1 week),'23:59:59')
-- 當月第一天的0點0分0秒

select timestamp(curdate()-day(curdate())+1)

-- 當月第一天的23點59分59秒

select timestamp(curdate()-day(curdate())+1,'23:59:59')

-- 當月最後一天的0點0分0秒

select timestamp(last_day(curdate()))

-- 當月最後一天的23點59分59秒

select timestamp(last_day(curdate()),'23:59:59')

-- 上月第一天的0點0分0秒

select timestamp(subdate(curdate()-day(curdate())+1,interval 1 month))

-- 上月第一天的23點59分59秒

select timestamp(subdate(curdate()-day(curdate())+1,interval 1 month),'23:59:59')

-- 上月最後一天的0點0分0秒

select timestamp(subdate(last_day(curdate()),interval 1 month))

-- 上月最後一天的23點59分59秒

select timestamp(subdate(last_day(curdate()),interval 1 month),'23:59:59')

-- 下月第一天的0點0分0秒

select timestamp(subdate(curdate()-day(curdate())+1,interval -1 month))

-- 下月第一天的23點59分59秒

select timestamp(subdate(curdate()-day(curdate())+1,interval -1 month),'23:59:59')

-- 下月最後一天的0點0分0秒

select timestamp(subdate(last_day(curdate()),interval -1 month))

-- 下月最後一天的23點59分59秒

select timestamp(subdate(last_day(curdate()),interval -1 month),'23:59:59')

-- 當年第一天的0點0分0秒

select timestamp(makedate(year(curdate()),1))

-- 當年第一天的23點59分59秒

select timestamp(makedate(year(curdate()),1),'23:59:59')

-- 當年最後一天的0點0分0秒

select timestamp(concat(year(curdate()),'1231'))

-- 當年最後一天的23點59分59秒

select timestamp(concat(year(curdate()),'1231'),'23:59:59')

-- 上年第一天的0點0分0秒

select timestamp(makedate(year(curdate())-1,1))

-- 當年第一天的23點59分59秒

select timestamp(makedate(year(curdate())-1,1),'23:59:59')

-- 上年最後一天的0點0分0秒

select timestamp(concat(year(curdate())-1,'1231'))

-- 當年最後一天的23點59分59秒

select timestamp(concat(year(curdate())-1,'1231'),'23:59:59')

-- 下年第一天的0點0分0秒

select timestamp(makedate(year(curdate())+1,1))

-- 下年第一天的23點59分59秒

select timestamp(makedate(year(curdate())+1,1),'23:59:59')

-- 下年最後一天的0點0分0秒

select timestamp(concat(year(curdate())+1,'1231'))

-- 下年最後一天的23點59分59秒

select timestamp(concat(year(curdate())+1,'1231'),'23:59:59')

上述寫法都有很多種,基於mysql自帶的日期函式,就可以隨意組合

mysql常用日期 時間查詢

好記性不如爛筆頭 select curdate 獲取當前日期 select last day curdate 獲取本月最後一天。select date add curdate interval day curdate 1day 獲取本月第一天 select date add curdate day ...

mysql日期查詢 mysql 查詢日期

檢視本月資料 select from content publish where date format publish time,y m date format date sub curdate interval 0 month y m 檢視上個月資料 select from content pu...

mysql 日期查詢 Mysql日期查詢list

當前week的第一天 select date sub curdate interval weekday curdate 1 day 當前week的最後一天 select date sub curdate interval weekday curdate 5 day 前一week的第一天 select...