(24) 返回指定位置的字符串的函数:
mysql> select elt(2,'a','b','c'); +--------------------+ | elt(2,'a','b','c') | +--------------------+ | b | +--------------------+ 1 row in set (0.00 sec)
(25) 返回指定字符串位置的函数:
mysql> select field('a','a','b','c'); +------------------------+ | field('a','a','b','c') | +------------------------+ | 1 | +------------------------+ 1 row in set (0.00 sec)
(26) 返回子字符串位置的函数
mysql> select find_in_set('c','a,b,c,d'); +----------------------------+ | find_in_set('c','a,b,c,d') | +----------------------------+ | 3 | +----------------------------+ 1 row in set (0.00 sec)
三,日期函数
(27) 获取当前日期的函数
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2023-04-14 | +------------+ 1 row in set (0.00 sec)
(28) 获取当前时间的函数
mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 08:54:47 | +-----------+ 1 row in set (0.00 sec)
(29) 获取当前日期和时间的函数
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2023-04-14 08:54:51 | +---------------------+ 1 row in set (0.00 sec)
(30) 获取时间戳的函数:
mysql> select unix_timestamp(); +------------------+ | unix_timestamp() | +------------------+ | 1681433706 | +------------------+ 1 row in set (0.00 sec)
(31) 转换时间戳的函数:
mysql> select from_unixtime(1681433706); +---------------------------+ | from_unixtime(1681433706) | +---------------------------+ | 2023-04-14 08:55:06 | +---------------------------+ 1 row in set (0.00 sec)
(32) 获取 UTC 日期的函数:
mysql> select utc_date; +------------+ | utc_date | +------------+ | 2023-04-14 | +------------+ 1 row in set (0.00 sec)
(33) 获取 UTC 时间的函数:
mysql> select utc_time; +----------+ | utc_time | +----------+ | 00:55:38 | +----------+ 1 row in set (0.00 sec)
(34) 获取月份的函数:
mysql> select month(now()); +--------------+ | month(now()) | +--------------+ | 4 | +--------------+ 1 row in set (0.00 sec)
(35) 获取星期的函数:
mysql> select dayname(now()); #英文显示第周几 +----------------+ | dayname(now()) | +----------------+ | Friday | +----------------+ 1 row in set (0.00 sec) mysql> select dayofweek(now()); #一周中的第多少天 1 表示周日,2 表示周一,...... ,7 表示周六 +------------------+ | dayofweek(now()) | +------------------+ | 6 | +------------------+ 1 row in set (0.00 sec) mysql> select weekday(now()); #0 表示周一,1 表示周二,...... ,6 表示周日 +----------------+ | weekday(now()) | +----------------+ | 4 | +----------------+ 1 row in set (0.00 sec)
(36) 获取天数的函数
mysql> select dayofyear(now()); #一年中的第多少天 +------------------+ | dayofyear(now()) | +------------------+ | 104 | +------------------+ 1 row in set (0.00 sec) mysql> select dayofmonth(now()); #一个月中的第多少天 +-------------------+ | dayofmonth(now()) | +-------------------+ | 14 | +-------------------+ 1 row in set (0.00 sec)
(37) 获取年份的函数:
mysql> select year(now()); +-------------+ | year(now()) | +-------------+ | 2023 | +-------------+ 1 row in set (0.00 sec)
(38) 获取季度的函数
mysql> select quarter(now()); +----------------+ | quarter(now()) | +----------------+ | 2 | +----------------+ 1 row in set (0.00 sec)
(39) 获取分钟的函数:
mysql> select minute(now()); +---------------+ | minute(now()) | +---------------+ | 57 | +---------------+ 1 row in set (0.00 sec)
(40) 获取秒钟的函数:
mysql> select second(now()); +---------------+ | second(now()) | +---------------+ | 10 | +---------------+ 1 row in set (0.00 sec)
(41) 获取日期的指定值的函数
mysql> select extract(year from now()); +--------------------------+ | extract(year from now()) | +--------------------------+ | 2023 | +--------------------------+ 1 row in set (0.00 sec) mysql> select extract(year_month from now()); +--------------------------------+ | extract(year_month from now()) | +--------------------------------+ | 202304 | +--------------------------------+ 1 row in set (0.00 sec) mysql> select extract(hour_minute from now()); +---------------------------------+ | extract(hour_minute from now()) | +---------------------------------+ | 859 | +---------------------------------+ 1 row in set (0.00 sec)
(42) 时间和秒钟转换的函数
mysql> select time_to_sec(now()); +--------------------+ | time_to_sec(now()) | +--------------------+ | 32874 | +--------------------+ 1 row in set (0.00 sec) mysql> select sec_to_time(32874); +--------------------+ | sec_to_time(32874) | +--------------------+ | 09:07:54 | +--------------------+ 1 row in set (0.00 sec)
(43) 计算日期和时间的函数
mysql> select date_add(now(),interval 1 year); #当前日期增加一年 +---------------------------------+ | date_add(now(),interval 1 year) | +---------------------------------+ | 2024-04-14 09:09:30 | +---------------------------------+ 1 row in set (0.00 sec) mysql> select date_sub(now(),interval 1 month); #当前日期减一个月 +----------------------------------+ | date_sub(now(),interval 1 month) | +----------------------------------+ | 2023-03-14 09:09:58 | +----------------------------------+ 1 row in set (0.00 sec) mysql> select datediff(now(),'2023-03-14 09:09:58'); #取两个时间的差 +---------------------------------------+ | datediff(now(),'2023-03-14 09:09:58') | +---------------------------------------+ | 31 | +---------------------------------------+ 1 row in set (0.00 sec)