mysql 日期函数

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

 select now(),current_date,current_time;

格式:

查询每个月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值:

%S, %s  两位数字形式的秒( 00,01, ..., 59)
%I, %i  两位数字形式的分( 00,01, ..., 59)
%H  两位数字形式的小时,24 小时(00,01, ..., 23)
%h  两位数字形式的小时,12 小时(01,02, ..., 12)
%k  数字形式的小时,24 小时(0,1, ..., 23)
%l  数字形式的小时,12 小时(1, 2, ..., 12)
%T  24 小时的时间形式(hh:mm:ss)
%r  12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p  AM或PM
%W  一周中每一天的名称(Sunday, Monday, ..., Saturday)
%a  一周中每一天名称的缩写(Sun, Mon, ..., Sat)
%d  两位数字表示月中的天数(00, 01,..., 31)
%e  数字形式表示月中的天数(1, 2, ..., 31)
%D  英文后缀表示月中的天数(1st, 2nd, 3rd,...)
%w  以数字形式表示周中的天数( 0 = Sunday, 1=Monday, ..., 6=Saturday)
%j  以三位数字表示年中的天数( 001, 002, ..., 366)
%U  周(0, 1, 52),其中Sunday 为周中的第一天
%u  周(0, 1, 52),其中Monday 为周中的第一天
%M  月名(January, February, ..., December)
%b  缩写的月名( January, February,...., December)
%m  两位数字表示的月份(01, 02, ..., 12)
%c  数字表示的月份(1, 2, ...., 12)
%Y  四位数字表示的年份
%y  两位数字表示的年份
%%  直接值“%”

posted @ 2019-11-02 10:48  superming168  阅读(203)  评论(0编辑  收藏  举报