Some time function records commonly used by mysql

  1. no parameter required

    1. NOW()| CURRENT_TIMESTAMP
      Return current time (date + hour, minute and second): 2021-12-27 12:12:12

    2. CURDATE()| CURRENT_DATE()| CURRENT_DATE
      Return to the current date: 2021-12-27

    3. CURTIME()| CURRENT_TIME
      Return current time: 12:12:12

  2. time conversion

    1. UNIX_TIMESTAMP(date), the date is converted to a timestamp
    2. FROM_UNIXTIME(date), the timestamp is converted to a date
    3. YEAR(date), returns the year
    4. WEEK(date), returns the week of the year
    5. HOUR(time), returns the hour value
    6. MINUTE(time), returns the minute value
    7. MONTHNAME(date), returns the month name of the date, such as December
    8. 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()); -- 微秒
      
  3. time calculation

    1. 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 type

      DATE_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

    1. TIMEDIFF(expr1, expr2): Returns the time difference between two dates subtracted (expr1 − expr2) (both parameter types must be the same)
    2. DATEDIFF(expr1, expr2): Returns the number of days between the subtraction of two dates (expr1 − expr2)
  4. personalized display

    1. The return date is the day of the week, January, year

      1. Day of the week:dayofweek(date)
      2. Day of January:dayofmonth(date)
      3. Day of the year:dayofyear(date)
    2. 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)
      

Guess you like

Origin blog.csdn.net/qq_43382853/article/details/122179608