mysql时间日期函数

匿名 (未验证) 提交于 2019-12-02 22:06:11

mysql时间日期函数

now(), current_timestamp();     -- 当前日期时间 current_date();                    -- 当前日期 current_time();                    -- 当前时间 date('yyyy-mm-dd hh:ii:ss');    -- 获取日期部分 time('yyyy-mm-dd hh:ii:ss');    -- 获取时间部分 date_format('yyyy-mm-dd hh:ii:ss', '%d %y %a %d %m %b %j');    -- 格式化时间 unix_timestamp();                -- 获得unix时间戳 from_unixtime();                -- 从时间戳获得时间

官网:https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_yearweek

格式:

查询每个月1号

select * from lagouok where right(date_format([字段],'%Y-%m-%d'),2)=1

获取星期几

select dayofweek(now())-1
dayofweek(字段)=1  查询每个月星期二
 

mysql根据生日查询年龄

select year(curdate())-year(字段)-(right(curdate(),5)<right(字段,5)) 年龄 from emp;

查询多少年前select date_sub(sysdate(),interval 10 year)

date_format([字段名称],'%Y-%m-%d') = curdate()

1、当前日期

select DATE_SUB(curdate(),INTERVAL 0 DAY) ;


2、明天日期
select DATE_SUB(curdate(),INTERVAL -1 DAY) ;


3、昨天日期

select adddate(now(),-1)
select date_sub(now(),interval 1 day)

4、前一个小时时间

select date_sub(now(), interval 1 hour);

5、后一个小时时间

select date_sub(now(), interval -1 hour);

6、前30分钟时间

select date_add(now(),interval -30 minute)

7、后30分钟时间

select date_add(now(),interval 30 minute)

8、根据format字符串格式化date值:

























now(), current_timestamp();     -- 当前日期时间 current_date();                    -- 当前日期 current_time();                    -- 当前时间 date('yyyy-mm-dd hh:ii:ss');    -- 获取日期部分 time('yyyy-mm-dd hh:ii:ss');    -- 获取时间部分 date_format('yyyy-mm-dd hh:ii:ss', '%d %y %a %d %m %b %j');    -- 格式化时间 unix_timestamp();                -- 获得unix时间戳 from_unixtime();                -- 从时间戳获得时间

官网:https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_yearweek

格式:

查询每个月1号

select * from lagouok where right(date_format([字段],'%Y-%m-%d'),2)=1

获取星期几

select dayofweek(now())-1
dayofweek(字段)=1  查询每个月星期二
 

mysql根据生日查询年龄

select year(curdate())-year(字段)-(right(curdate(),5)<right(字段,5)) 年龄 from emp;

查询多少年前select date_sub(sysdate(),interval 10 year)

date_format([字段名称],'%Y-%m-%d') = curdate()

1、当前日期

select DATE_SUB(curdate(),INTERVAL 0 DAY) ;


2、明天日期
select DATE_SUB(curdate(),INTERVAL -1 DAY) ;


3、昨天日期

select adddate(now(),-1)
select date_sub(now(),interval 1 day)

4、前一个小时时间

select date_sub(now(), interval 1 hour);

5、后一个小时时间

select date_sub(now(), interval -1 hour);

6、前30分钟时间

select date_add(now(),interval -30 minute)

7、后30分钟时间

select date_add(now(),interval 30 minute)

8、根据format字符串格式化date值:

























易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!