Mysql gets some function records of time

Mysql function to get date and time

mysql datetime functions describe
NOW([fps]) / CURRENT_TIMESTAMP([fps]) / LOCALTIMESTAMP(fps) / LOCALTIME([fps]) Return the current date and time ( datetime ), (parameter fpsis optional, the default is 2, second precision, value [0,6])
CURDATE() / CURRENT_DATE() Returns the current date ( date ), only contains the year, month, and day, in the default format:YYYY-MM-DD
CURTIME([fps])/CURRENT_TIME([fps]) Returns the current time ( time ), which only includes hours, minutes, and seconds (parameters fpsare optional, default is 2, seconds precision, value [0,6]), default format:YYYY-MM-DD HH:MM:SS
UNIX_TIMESTAMP(date) Returns the UNIX timestamp of the current date date
FROM_UNIXTIME Returns the date value of a UNIX timestamp
MONTHNAME(date) Returns the English name of the month of the date date
SELECT DAYNAME(NOW()) Returns the English name of the day of the week of date
DATE_FORMAT(date, fmt) Returns the date value formatted by the string fmt
DATE_ADD(date, INTERVAL expr unit) Returns a date/time date plus an interval expr.
DATE_SUB(date, INTERVAL expr unit) Returns a date/time date minus an interval expr.
DATEDIFF(expr1, expr1) Returns the number of days between the start time expr1 and the end time expr2
TIMEDIFF(expr1, expr2) Returns the number of days between two dates (time) date (expr1 - expr2)

Function name describe
YEAR(expr) Returns the year part (format yyyy) of the specified date and time, case: SELECT YEAR(NOW()), returns the year of the current time
MONTH(expr) Returns the month part of the specified datetime
DAY(expr) Returns the **date** part of the specified datetime
HOUR(expr) Returns the hour part of the specified datetime
MINUTE(expr) Returns the minutes portion of the specified datetime
SECOND(expr) Returns the seconds portion of the specified datetime
MICROSECOND(expr) Returns the microseconds part of the specified datetime
QUARTER(expr) Returns the quarter part of the specified datetime
WEEK(expr) Returns the week portion of the specified datetime
DATE(expr) Returns the date part of the specified datetime
TIME(expr) Returns the time part of the specified datetime

1. The main function to get the current datetime (datetime): NOW()andSYSDATE()

The difference between functions NOW()and SYSDATE():

NOW()Both SYSDATE()represent the current time, but there is a little difference in usage.
NOW()What is taken is the time when the statement starts to execute , and SYSDATE()what is taken is the dynamic real-time time .
This is because NOW()is taken from a variable of mysql TIMESTAMP, and this variable is set when the statement starts to execute, so it will not change during the entire statement execution.
For example, the following SQL query:

SELECT
	NOW(),SYSDATE(),
	SLEEP(2) AS '等待2s',
	NOW(),SYSDATE()

Query result:
insert image description here
It is found that the time of the two queries NOW()is the same, but SYSDATE()the time of the two times is different, which is a dynamic real-time time.
We generally use NOW()instead of SYSDATE()the function

There are also three functions to get the time, the effect is the NOW()same as that of

LOCALTIME()
LOCALTIMESTAMP()
CURRENT_TIMESTAMP()

NOW([fps])function

Return the current date and time of the server (datetime object)
parameter fspoptional, used to specify the precision of fractional seconds, if not written, it will display 2 digits by default (value range [0,6])

The NOW() function returns two formats by default: YYYY-MM-DD HH:MM:SSorYYYYMMDDHHMMSS

NOW(): return YYYY-MM-DD HH:MM:SSformat
NOW(): return YYYYMMDDHHMMSSformat

select NOW(), NOW()+0, NOW(0), NOW(6)

run:
insert image description here


2. Calculation function of date and time (addition and subtraction time interval)

DATE_ADD(date, INTERVAL expr type)and DATE_SUB(date, INTERVAL expr type)are the values ​​after increasing/decreasing the time, respectively.

INTERVALIt is a keyword, indicating the type of interval.
exprIt is an expression, that is, the value of the interval
unit. It is the unit of the time interval. It mainly has the following value units (year, month, day, hour, minute, second, etc.)

The value of unit

unit time interval unit value describe
YEAR Year
MONTH moon
DAY Day
HOUR hour
MINUTE point
SECOND Second
MICROSECOND millisecond
QUARTER season (3 months)
WEEK week
YEAR_MONTH year and month
DAY_HOUR day and time
DAY_MINUTE weather forecast
DAY_ SECOND day and second
HOUR_MINUTE hours and minutes
HOUR_SECOND hours and seconds
MINUTE_SECOND minutes and seconds
-- 给当前时间加三小时
SELECT NOW(),DATE_ADD(NOW(),interval 1+2 HOUR)

insert image description here

-- 2021-03-18 00:00:00 加18小时18分后的时间
SELECT DATE_ADD('2021-03-18 00:00:00',interval '18:18' HOUR_MINUTE)

insert image description here

Function EXTRACT(unit FROM datetime)to extract a single part or a combination of parts from a datetime.

for example:

-- 从当前的日期时间中,抽取出 时分 组合部分
select EXTRACT(HOUR_MINUTE FROM NOW())

insert image description here

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/114887455