-
no parameter required
-
NOW()
|CURRENT_TIMESTAMP
Return current time (date + hour, minute and second): 2021-12-27 12:12:12 -
CURDATE()
|CURRENT_DATE()
|CURRENT_DATE
Return to the current date: 2021-12-27 -
CURTIME()
|CURRENT_TIME
Return current time: 12:12:12
-
-
time conversion
UNIX_TIMESTAMP(date)
, the date is converted to a timestampFROM_UNIXTIME(date)
, the timestamp is converted to a dateYEAR(date)
, returns the yearWEEK(date)
, returns the week of the yearHOUR(time)
, returns the hour valueMINUTE(time)
, returns the minute valueMONTHNAME(date)
, returns the month name of the date, such as December- Select various parts of a datetime: date, time, year, quarter, month, day, hour, minute, second, microsecond (commonly used)
SELECT now(),date(now()); -- 日期 SELECT now(),time(now()); -- 时间 SELECT now(),year(now()); -- 年 SELECT now(),quarter(now()); -- 季度 SELECT now(),month(now()); -- 月 SELECT now(),week(now()); -- 周 SELECT now(),day(now()); -- 日 SELECT now(),hour(now()); -- 小时 SELECT now(),minute(now()); -- 分钟 SELECT now(),second(now()); -- 秒 SELECT now(),microsecond(now()); -- 微秒
-
time calculation
-
Add (add) or subtract (sub) an interval value expr to the given date date respectively
interval is the interval type keyword
expr is an expression corresponding to the following typeDATE_ADD(date, INTERVAL expr unit);
DATE_SUB(date, INTERVAL expr unit);unit is the time interval unit, and the time interval types are as follows
unit describe HOUR Hour MINUTE point SECOND Second MICROSECOND millisecond YEAR Year MONTH moon DAY Day WEEK week QUARTER the quarter YEAR_MONTH year and month DAY_HOUR day and hour DAY_MINUTE day and minute DAY_SECOND day and second HOUR_MINUTE hours and minutes HOUR_SECOND hours and seconds MINUTE_SECOND minutes and seconds example
-- 获取上个月第一天和最后一天 SELECT DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m-01'), DATE_SUB(DATE_FORMAT(now(), '%Y-%m-01'), INTERVAL 1 day);
2. Get the time difference or the difference in days between two times
- TIMEDIFF(expr1, expr2): Returns the time difference between two dates subtracted (expr1 − expr2) (both parameter types must be the same)
- DATEDIFF(expr1, expr2): Returns the number of days between the subtraction of two dates (expr1 − expr2)
-
-
personalized display
-
The return date is the day of the week, January, year
- Day of the week:
dayofweek(date)
- Day of January:
dayofmonth(date)
- Day of the year:
dayofyear(date)
- Day of the week:
-
Returns the day of the week and month name for a date
The name is Chinese or English controlled by the system variable lc_time_names (default value is 'en_US')
mysql> show variables like 'lc_time_names'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | lc_time_names | en_US | +---------------+-------+ 1 row in set (0.00 sec) mysql> select dayname(now()),monthname(now()); +----------------+------------------+ | dayname(now()) | monthname(now()) | +----------------+------------------+ | Monday | January | +----------------+------------------+ 1 row in set (0.00 sec) mysql> set lc_time_names='zh_CN'; Query OK, 0 rows affected (0.00 sec) mysql> select dayname(now()),monthname(now()); +----------------+------------------+ | dayname(now()) | monthname(now()) | +----------------+------------------+ | 星期一 | 一月 | +----------------+------------------+ 1 row in set (0.00 sec)
-
Some time function records commonly used by mysql
Guess you like
Origin blog.csdn.net/qq_43382853/article/details/122179608
Recommended
Ranking