当前位置:网站首页>Time related operations commonly used in MySQL
Time related operations commonly used in MySQL
2022-07-21 17:20:00 【Running snail - calm starts with me】
mysql Common time related operations
List of articles
- mysql Common time related operations
- Return current date :CURRENT_DATE()
- Return current time :CURRENT_TIME()
- Returns the current date and time :NOW()
- Returns the current date and time :CURRENT_TIMESTAMP()
- Timestamp conversion :UNIX_TIMESTAMP($date)
- Timestamp formatting :FROM_UNIXTIME( u n i x t i m e s t a m p , unix_timestamp, unixtimestamp,format_type)
- Return year : YEAR($date)
- Return to the month :MONTH($date)
- Return the number :DAY($date)
- return :HOUR($date)
- Return points :MINUTE($date)
- Return seconds :SECOND($date)
- Format time : DATE_FORMAT( d a t e , date, date,format_type)
- Time increases :DATE_ADD($date,interval $index $date_type)
- Time reduction :DATE_SUB($date,interval $index $date_type)
- Time difference :DATEDIFF( e n d d a t e , end_date, enddate,begin_date)
- Time difference :TIMESTAMPDIFF( d a t e t y p e , date_type, datetype,begin_date, $end_date)
- Time difference :TIMEDIFF( e n d d a t e , end_date, enddate,begin_date)
Return current date :CURRENT_DATE()
select CURRENT_DATE() from date_test
CURRENT_DATE() |
---|
2022-07-14 |
Return current time :CURRENT_TIME()
select CURRENT_TIME() from date_test
CURRENT_TIME() |
---|
17:42:59 |
Returns the current date and time :NOW()
select NOW() from date_test
NOW() |
---|
2022-07-14 17:43:35 |
Returns the current date and time :CURRENT_TIMESTAMP()
select CURRENT_TIMESTAMP() from date_test
CURRENT_TIMESTAMP() |
---|
2022-07-14 17:43:57 |
Timestamp conversion :UNIX_TIMESTAMP($date)
$date: Specified date time
select UNIX_TIMESTAMP(now()) from date_test # Convert the current date to a timestamp
UNIX_TIMESTAMP(now()) |
---|
1657791851 |
Timestamp formatting :FROM_UNIXTIME( u n i x t i m e s t a m p , unix_timestamp, unixtimestamp,format_type)
$unix_timestamp The specified time stamp
$format_type Can be used in format_type in :
%M The name of the month (January……December)
%W The name of the week (Sunday……Saturday)
%D The date of the month with the English prefix (1st, 2nd, 3rd, wait .)
%Y year , Numbers , 4 position
%y year , Numbers , 2 position
%a Abbreviated week name (Sun……Sat)
%d Days in a month , Numbers (00……31)
%e Days in a month , Numbers (0……31)
%m month , Numbers (01……12)
%c month , Numbers (1……12)
%b Abbreviated month name (Jan……Dec)
%j Days of the year (001……366)
%H Hours (00……23)
%k Hours (0……23)
%h Hours (01……12)
%I Hours (01……12)
%l Hours (1……12)
%i minute , Numbers (00……59)
%r Time ,12 Hours (hh:mm:ss [AP]M)
%T Time ,24 Hours (hh:mm:ss)
%S second (00……59)
%s second (00……59)
%p AM or PM
%w Days of the week (0=Sunday ……6=Saturday )
%U week (0……52), Here Sunday is the first day of the week
%u week (0……52), Monday is the first day of the week
select FROM_UNIXTIME(1657791851,'%Y-%c-%d %h:%i:%s') from date_test # Returns the current date
FROM_UNIXTIME(1657791851,‘%Y-%c-%d %h:%i:%s’) |
---|
2022-7-14 05:44:11 |
Return year : YEAR($date)
$date: Specified date time
select YEAR(now()) from date_test # Returns the current year
YEAR(now()) |
---|
2022 |
Return to the month :MONTH($date)
$date: Specified date time
select MONTH(now()) from date_test # Returns the current month
MONTH(now()) |
---|
7 |
Return the number :DAY($date)
$date: Specified date time
select DAY(now()) from date_test # Returns the current day
DAY(now()) |
---|
14 |
return :HOUR($date)
$date: Specified date time
select HOUR(now()) from date_test # Returns the current time
HOUR(now()) |
---|
17 |
Return points :MINUTE($date)
$date: Specified date time
select MINUTE(now()) from date_test # Returns the current score
MINUTE(now()) |
---|
48 |
Return seconds :SECOND($date)
$date: Specified date time
select SECOND(now()) from date_test # Returns the current second
SECOND(now()) |
---|
39 |
Format time : DATE_FORMAT( d a t e , date, date,format_type)
ps: Convert time to specified format
$date: Specified date time
$format_type Can be used in format_type in :
%M The name of the month (January……December)
%W The name of the week (Sunday……Saturday)
%D The date of the month with the English prefix (1st, 2nd, 3rd, wait .)
%Y year , Numbers , 4 position
%y year , Numbers , 2 position
%a Abbreviated week name (Sun……Sat)
%d Days in a month , Numbers (00……31)
%e Days in a month , Numbers (0……31)
%m month , Numbers (01……12)
%c month , Numbers (1……12)
%b Abbreviated month name (Jan……Dec)
%j Days of the year (001……366)
%H Hours (00……23)
%k Hours (0……23)
%h Hours (01……12)
%I Hours (01……12)
%l Hours (1……12)
%i minute , Numbers (00……59)
%r Time ,12 Hours (hh:mm:ss [AP]M)
%T Time ,24 Hours (hh:mm:ss)
%S second (00……59)
%s second (00……59)
%p AM or PM
%w Days of the week (0=Sunday ……6=Saturday )
%U week (0……52), Here Sunday is the first day of the week
%u week (0……52), Monday is the first day of the week
select DATE_FORMAT('2022-07-06 10:04:35','%Y-%m-%d') from date_test # Returns the current second
DATE_FORMAT(‘2022-07-06 10:04:35’,‘%Y-%m-%d’) |
---|
2022-07-06 |
Time increases :DATE_ADD($date,interval $index $date_type)
ps: The returned date and time
$date: For the specified date
$index: Specific values , For example, increase 1 God , Then for 1
$date_type : Time type , The value is
day: God
hour: when
minute: branch
second: second
week: Zhou
month: month
quarter: quarter
year: year
select DATE_ADD('2022-07-05 00:59:59', interval 2 day) as result from date_test # Two more days
result |
---|
2022-07-07 00:59:59 |
Time reduction :DATE_SUB($date,interval $index $date_type)
ps: The returned date and time
$date: For the specified date
$index: Specific values , For example, reduce 1 God , Then for 1
$date_type : Time type , The value is
day: God
hour: when
minute: branch
second: second
week: Zhou
month: month
quarter: quarter
year: year
select DATE_SUB('2022-07-05 00:59:59', interval 2 day) as result from date_test # Two days less
result |
---|
22022-07-03 00:59:59 |
Time difference :DATEDIFF( e n d d a t e , end_date, enddate,begin_date)
ps: Returns the subtraction of two dates and times , Get the difference in days ,
Be careful : Time ( Minutes and seconds ) Will not participate in the calculation
$end_date: End time
$begin_date: Start time
select DATEDIFF('2022-07-09 00:59:59', '2022-07-07 12:59:59') as result from date_test # Subtract two times to get days
result |
---|
2 |
Time difference :TIMESTAMPDIFF( d a t e t y p e , date_type, datetype,begin_date, $end_date)
ps: Returns the subtraction of two dates and times , Get the time value of the specified type
Be careful : Time ( Minutes and seconds ) Will participate in the calculation
$end_date: End time
$begin_date: Start time
$date_type: The value is
day: God
hour: when
minute: branch
second: second
week: Zhou
month: month
quarter: quarter
year: year
select TIMESTAMPDIFF(day, '2022-07-07 12:59:59','2022-07-09 00:59:59') as result from date_test
result |
---|
1 |
Time difference :TIMEDIFF( e n d d a t e , end_date, enddate,begin_date)
ps: Returns the subtraction of two dates and times , Get a time difference
Be careful : Time ( Minutes and seconds ) Will participate in the calculation
$end_date: End time
$begin_date: Start time
select TIMEDIFF('2022-07-09 00:59:59', '2022-07-07 12:59:59') as result from date_test # Subtract two times to get time
result |
---|
36:00:00 |
^ ^
-
边栏推荐
猜你喜欢
有了线程后,发生的变化
看起来是线程池的BUG,但是我认为是源码设计不合理。
2022-07-20 第五小组 修身课 学习笔记(every day)
第03篇:SQL语法树解析
The remote control software should also have plan B alternatives
JS--循环--猜数字(生成随机数存在小数Math.random)
Kubernetes Service 负载均衡实现机制
卷积神经网络单图超分辨率的深度学习方法
CDH集群 不良 : 群集中有 1,855 个 副本不足的块 块。群集中共有 1,857 个块。百分比 副本不足的块: 99.89%。 临界阈值:40.00%。
4种 Redis 集群方案及优缺点对比
随机推荐
This year, if you endure, you will have hope
When using mysql, please make good use of JSON
二叉树的三种层序遍历BFS
泄漏电流、无接地保护措施……1296个进口电热水壶存在重大安全隐患
kube-Controller Manager 原理
Kubernetes Service 负载均衡实现机制
12、用户微服务
Analysis of tars source code 24
进程控制块(PCB) 包含哪些信息
BCG grid control sets color separately
Splunk HEC Open 8088 port
Analysis of tars source code 21
为什么用了大牌工具后报表开发依然头痛
Analysis of tars source code 26
看起来是线程池的BUG,但是我认为是源码设计不合理。
ARIMA疫情期间港口靠挂数的时间序列分析
第03篇:SQL语法树解析
思科配置单臂路由的实际案例,很简单
It seems to be a bug of thread pool, but I think the source code design is unreasonable.
The performance of major mainstream programming languages is PK, and the results are unexpected