mysql 日期总结

select to_days(now()) #737733

select date_format(now(),'%Y-%m-%d') #2019-11-05

select CURRENT_DATE() #2019-11-05
select CURDATE(); #2019-11-05
select NOW(); #2019-11-05 21:29:20

select date(now()) #2019-11-05

###一、mysql查询今天、昨天、7天、近30天、本月、上一月 数据
#今天
select * from 表名 where to_days(时间字段名) = to_days(now());

select * from table where date(column_time) = curdate();

select * from table where a.dtCreateTime> DATE_FORMAT(now(),'%Y-%m-%d') and a.dtCreateTime < DATE_FORMAT(date_add(now(),interval 1 day),'%Y-%m-%d')

select * from table where dtCreateTime >= CURRENT_DATE () AND dtCreateTime < date_add(CURRENT_DATE, INTERVAL 1 DAY)

#昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <= 1

dtCreateTime >= date_add(CURRENT_DATE, INTERVAL -1 DAY) AND dtCreateTime < CURRENT_DATE ()

#近7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

#近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

select * from table where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(column_time);

#本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )

#上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 时间字段名, ‘%Y%m’ ) ) =1

####二、查询某一天纪录

查询当天记录

1、select * from table where date(regdate) = curdate();

date()函数获取日期部分, 扔掉时间部分,然后与当前日期比较即可

2、select * from tbname where date_format(tbname.colstime,'%Y-%m-%d')=now()

3、如果想查某一天的,

date > '2012-3-12' and date < '2012-3-13'

或:

where date(op_date)='2012-03-09'

三、mysql查询时间戳和日期的转换
from_unixtime(time_stamp) -> 将时间戳转换为日期

unix_timestamp(date) -> 将指定的日期或者日期字符串转换为时间戳

select from_unixtime(1382544000) SELECT unix_timestamp(now()) #1572995825
如果要查询当天的订单的记录:

select count(*) from b_order

Where date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')

也可以这样:
select count(*) from b_order
Where create_time >= unix_timestamp('2013-10-24 00:00:00') and create_time <= unix_timestamp('2013-10-24 23:59:59') ;
建表的代码为:
create table t8 (
`id1` timestamp NOT NULL default CURRENT_TIMESTAMP,
`id2` datetime default NULL
);

1、 拼凑日期、时间函数:

makdedate(year,dayofyear), maketime(hour,minute,second)

select makedate(2001,31); -- '2001-01-31'

select makedate(2001,32); -- '2001-02-01'

select maketime(12,15,30); -- '12:15:30'

select date_format('2008-08-08 22:23:00', '%W %M %Y'); #Friday August 2008
select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s'); #20080808222301
select time_format('22:23:01', '%H.%i.%s'); #22.23.01

3、Str to Date (字符串转换为日期)

函数:str_to_date(str, format)

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

4、(日期、天数)转换

to_days(date), from_days(days)
一天内不同时间,to_days值相同
select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627
select from_days(0); -- '0000-00-00'
select from_days(733627); -- '2008-08-08'

5、日期、时间相减
datediff(date1,date2), timediff(time1,time2)

MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。
select datediff('2008-08-08', '2008-08-01'); -- 7
select datediff('2008-08-01', '2008-08-08'); -- -7

MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08

6、为日期减去一个时间间隔:
date_sub()
select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
#1997-12-30 22:58:59
select DATE_SUB(CURDATE(), INTERVAL 7 DAY); -- 2014-11-27 (7天前日期)

7、为日期增加一个时间间隔:
date_add()
set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); -- sub 1 day

select date_add(@dt, interval '01:15:30' hour_second); #2008-08-09 13:28:03
select date_add(@dt, interval '1 01:15:30' day_second); #2008-08-10 13:28:03

8、 last_day() 函数
返回月份中的最后一天。
select last_day('2008-02-01'); -- 2008-02-29
select last_day('2008-08-08'); -- 2008-08-31

9、week... 函数:
week(), weekofyear(), dayofweek(), weekday(), yearweek()

更多参考:
https://www.iteye.com/blog/uule-2159811 mysql 日期总结

原文地址:https://www.cnblogs.com/shy1766IT/p/11355785.html