TimeStamp () function, TimeStampAdd () function, TimeStampDiff () function

 

Official website: https: //dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff

 

TIMESTAMP(expr)TIMESTAMP(expr1,expr2)

With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.

PS: only one parameter, the parameter when the type of date or datetime, the final result is returned datetime

When there are two parameters, the second parameter is a time type, on the value of the second parameter is applied to the first parameter value

mysql> SELECT TIMESTAMP('2003-12-31');
        -> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
        -> '2004-01-01 00:00:00'
# Understand, i.e. the value of parameter 1 2 after the time parameter value, a value corresponding to a datetime
 SELECT  timestamp ( ' 2019-11-7 12:23:22 ' , ' 3:14:14 ' ) AS after a, # 2019. 11 years 7, 12:23:22, over 3 hours 14 minutes 14 seconds, the current time value date
 timestamp ( ' 2019-11-7 12:23:22 ' , ' . 19: 14:14 ' ) AS B, # that after 19: 14 : 14, the day over
 timestamp ( ' 2019-11-7 7:23:22 ' , ' 19:14:14 ' ) AS C;

 

 

 

 

TIMESTAMPADD(unit,interval,datetime_expr)

Adds the integer expression interval to the date or datetime expression datetime_expr. The unit for interval is given by theunit argument, which should be one of the following values: MICROSECOND (microseconds), SECONDMINUTEHOURDAYWEEK,MONTHQUARTER, or YEAR.

The unit value may be specified using one of keywords as shown, or with a prefix of SQL_TSI_. For example, DAY and SQL_TSI_DAY both are legal.

 

mysql> SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');
        -> '2003-01-02 00:01:00'
mysql> SELECT TIMESTAMPADD(WEEK,1,'2003-01-02');
        -> '2003-01-09'

 

 

 

 

 

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00'where necessary. The unit for the result (an integer) is given by the unit argument. The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.

 PS: Returns the latter - the former value, when the incoming parameter is date type, it will be in the system as datetime type process, the time is 00:00:00.

First parameter and the same TIMESTAMPADD: MICROSECOND (microseconds),  SECONDMINUTEHOURDAYWEEK, MONTHQUARTER, or  YEAR.

mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
        -> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
        -> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
        -> 128885

 

Note

The order of the date or datetime arguments for this function is the opposite of that used with theTIMESTAMP() function when invoked with 2 arguments.

 invoke: vt, call, pray, cause, pleading

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/bravesunforever/p/11816098.html