阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL常用函数

110次阅读
没有评论

共计 21947 个字符,预计需要花费 55 分钟才能阅读完成。

MySQL 函数,是一种控制流程函数,属于数据库用语言。

MySQL 数据库中提供了很丰富的函数。MySQL 函数包括数学函数、字符串函数、日期和时间函数、条件判断函数、系统信息函数、加密函数、格式化函数等。通过这些函数,可以简化用户的操作。

一、数学函数

1.1、函数概述

MySQL 函数是 MySQL 数据库提供的内部函数。这些内部函数可以帮助用户更加方便的处理表中的数据。

1.2、数学函数概述

数学函数是 MySQL 中常用的一类函数。主要用于处理数字,包括整型、浮点数等。

1.3、常用数学函数

1.3.1、abs()

abs(X): 返回 X 的绝对值

mysql> select abs(-32);
+----------+
| abs(-32) |
+----------+
| 32       |
+----------+
1 行于数据集 (0.03 秒)

1.3.2、mod()

MOD(N,M) 或 %: 返回 N 被 M 除的余数。

mysql> select mod(15,7);
+-----------+
| mod(15,7) |
+-----------+
| 1         |
+-----------+
1 行于数据集 (0.02 秒)

mysql> select 15%7;
+------+
| 15%7 |
+------+
| 1    |
+------+
1 行于数据集 (0.02 秒)

1.3.3、ceiling()

CEILING(X): 返回不小于 X 的最小整数值。

mysql> select ceiling(1.23);
+---------------+
| ceiling(1.23) |
+---------------+
| 2             |
+---------------+
1 行于数据集 (0.02 秒)

mysql> select ceiling(-1.23);
+----------------+
| ceiling(-1.23) |
+----------------+
| -1             |
+----------------+
1 行于数据集 (0.02 秒)

1.3.4、round()

ROUND(X) : 返回参数 X 的四舍五入的一个整数。

mysql> select round(3.58);
+-------------+
| round(3.58) |
+-------------+
| 4           |
+-------------+
1 行于数据集 (0.02 秒)

mysql> select round(-3.58);
+--------------+
| round(-3.58) |
+--------------+
| -4           |
+--------------+
1 行于数据集 (0.01 秒)

1.3.5、pi()

PI():返回圆周率 π,默认显示 6 位小数

mysql> select pi();
+----------+
| pi()     |
+----------+
| 3.141593 |
+----------+
1 行于数据集 (0.03 秒)

1.2.6、sqrt()

SQRT(x):返回非负数的 x 的二次方根

mysql> select sqrt(16);
+----------+
| sqrt(16) |
+----------+
| 4        |
+----------+
1 行于数据集 (0.03 秒)

1.3.7、ceil()

CEIL(x):返回不小于 x 的最小整数

mysql> select ceil(1.3);
+-----------+
| ceil(1.3) |
+-----------+
| 2         |
+-----------+
1 行于数据集 (0.01 秒)

mysql> select ceil(-1.3);
+---------------+
| ceiling(-1.3) |
+---------------+
| -1            |
+---------------+
1 行于数据集 (0.01 秒)

1.3.8、floor()

FLOOR(x):返回不大于 x 的最大整数

mysql> select floor(1.3);
+------------+
| floor(1.3) |
+------------+
| 1          |
+------------+
1 行于数据集 (0.01 秒)

mysql> select floor(-1.3);
+-------------+
| floor(-1.3) |
+-------------+
| -2          |
+-------------+
1 行于数据集 (0.01 秒)

1.3.9、round()

ROUND(x)、ROUND(x,y)

前者返回最接近于 x 的整数,即对 x 进行四舍五入;后者返回最接近 x 的数,其值保留到小数点后面 y 位,若 y 为负值,则将保留到 x 到小数点左边 y 位

mysql> select round(1.3555);
+---------------+
| round(1.3555) |
+---------------+
| 1             |
+---------------+
1 行于数据集 (0.01 秒)

mysql> select round(1.3555,2);
+-----------------+
| round(1.3555,2) |
+-----------------+
| 1.36            |
+-----------------+
1 行于数据集 (0.02 秒)

1.3.10、sign()

SIGN(x): 返回参数 x 的符号,- 1 表示负数,0 表示 0,1 表示正数

mysql> select sign(5);
+---------+
| sign(5) |
+---------+
| 1       |
+---------+
1 行于数据集 (0.02 秒)

mysql> select sign(-5);
+----------+
| sign(-5) |
+----------+
| -1       |
+----------+
1 行于数据集 (0.02 秒)

mysql> select sign(0);
+---------+
| sign(0) |
+---------+
| 0       |
+---------+
1 行于数据集 (0.02 秒)

1.3.11、pow(x,y)

POW(x,y) 和、POWER(x,y): 返回 x 的 y 次乘方的值

mysql> select pow(2,4);
+----------+
| pow(2,4) |
+----------+
| 16       |
+----------+
1 行于数据集 (0.01 秒)

mysql> select power(3,3);
+------------+
| power(3,3) |
+------------+
| 27         |
+------------+
1 行于数据集 (0.02 秒)

1.3.12、rand()

RAND():随机函数,返回 0 - 1 内的随机数

mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.30107216378773766 |
+---------------------+
1 行于数据集 (0.03 秒)

mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.37762552907469266 |
+---------------------+
1 行于数据集 (0.01 秒)

1.3.13、truncate()

TRUNCATE(x,Y): 数值截取,返回数值 x 截取 y 位小数的结果(不四舍五入)

mysql> select truncate(3.1415926,2);
+-----------------------+
| truncate(3.1415926,2) |
+-----------------------+
| 3.14                  |
+-----------------------+
1 行于数据集 (0.01 秒)

mysql> select truncate(3.1415926,4);
+-----------------------+
| truncate(3.1415926,4) |
+-----------------------+
| 3.1415                |
+-----------------------+
1 行于数据集 (0.01 秒)

二、字符串函数

2.1、字符串函数概述

字符串函数是 MySQL 中常用的一类函数。主要用于处理字符串。

2.2、常用字符串函数

2.2.1、ascii()

ASCII(str): 返回字符串 str 的最左面字符的 ASCII 代码值。如果 str 是空字符串,返回 0。如果 str 是 NULL,返回 NULL。

mysql> select ascii('2');
+------------+
| ascii('2') |
+------------+
| 50         |
+------------+
1 行于数据集 (0.01 秒)

mysql> select ascii(2);
+----------+
| ascii(2) |
+----------+
| 50       |
+----------+
1 行于数据集 (0.01 秒)

mysql> select ascii('Ax');
+-------------+
| ascii('Ax') |
+-------------+
| 65          |
+-------------+
1 行于数据集 (0.02 秒)

mysql> select ascii('ax');
+-------------+
| ascii('ax') |
+-------------+
| 97          |
+-------------+
1 行于数据集 (0.02 秒)

2.2.2、concat()

CONCAT(str1,str2,…): 返回来自于参数连结的字符串。如果任何参数是 NULL,返回 NULL。可以有超过 2 个的参数。一个数字参数被变换为等价的字符串形式。

mysql> select concat('hello','world','!');
+-----------------------------+
| concat('hello','world','!') |
+-----------------------------+
| helloworld!                 |
+-----------------------------+
1 行于数据集 (0.02 秒)

mysql> select concat('hello',null,'world');
+------------------------------+
| concat('hello',null,'world') |
+------------------------------+
| NULL                         |
+------------------------------+
1 行于数据集 (0.04 秒)

mysql> select concat(12,21);
+---------------+
| concat(12,21) |
+---------------+
| 1221          |
+---------------+
1 行于数据集 (0.02 秒)

2.2.3、length()

LENGTH(str): 获取字符串字节长度(返回字节数,要注意字符集)

mysql> select length('hello world');
+-----------------------+
| length('hello world') |
+-----------------------+
| 11                    |
+-----------------------+
1 行于数据集 (0.02 秒)

mysql> select length('你好');
+--------------+
| length('你好') |
+--------------+
| 6            |
+--------------+
1 行于数据集 (0.02 秒)

注意:

一个汉字是算三个字节,一个数字或字母算一个字节

2.2.4、locate()

LOCATE(substr,str): 返回子串 substr 在字符串 str 第一个出现的位置,如果 substr 不是在 str 里面,返回 0.

mysql> select locate('wo','hello world');
+----------------------------+
| locate('wo','hello world') |
+----------------------------+
| 7                          |
+----------------------------+
1 行于数据集 (0.04 秒)

mysql> select locate('wob','hello world');
+-----------------------------+
| locate('wob','hello world') |
+-----------------------------+
| 0                           |
+-----------------------------+
1 行于数据集 (0.02 秒)

2.2.5、instr()

INSTR(str,substr): 返回子串 substr 在字符串 str 中的第一个出现的位置。

mysql> select instr('hello world','o');
+--------------------------+
| instr('hello world','o') |
+--------------------------+
| 5                        |
+--------------------------+
1 行于数据集 (0.01 秒)

mysql> select instr('hello world','ob');
+---------------------------+
| instr('hello world','ob') |
+---------------------------+
| 0                         |
+---------------------------+
1 行于数据集 (0.01 秒)

2.2.6、left()

LEFT(str,len): 返回字符串 str 的最左面 len 个字符。

mysql> select left('hello world',5);
+-----------------------+
| left('hello world',5) |
+-----------------------+
| hello                 |
+-----------------------+
1 行于数据集 (0.01 秒)

2.2.7、right()

RIGHT(str,len): 返回字符串 str 的最右面 len 个字符。

mysql> select right('hello world',5);
+------------------------+
| right('hello world',5) |
+------------------------+
| world                  |
+------------------------+
1 行于数据集 (0.01 秒)

2.2.8、substring()

SUBSTRING(str,pos): 从字符串 str 的起始位置 pos 返回一个子串。

mysql> select substring('hello world',5);
+----------------------------+
| substring('hello world',5) |
+----------------------------+
| o world                    |
+----------------------------+
1 行于数据集 (0.01 秒)
mysql> select substring('hello world',2,6);
+------------------------------+
| substring('hello world',2,6) |
+------------------------------+
| ello w                       |
+------------------------------+
1 行于数据集 (0.01 秒)

mysql> select substring('hello world' from 7);
+---------------------------------+
| substring('hello world' from 7) |
+---------------------------------+
| world                           |
+---------------------------------+
1 行于数据集 (0.01 秒)

mysql> select substring('hello world' from 7 for 2);
+---------------------------------------+
| substring('hello world' from 7 for 2) |
+---------------------------------------+
| wo                                    |
+---------------------------------------+

mysql> select substring('hello world' from -3 for 2);
+----------------------------------------+
| substring('hello world' from -3 for 2) |
+----------------------------------------+
| rl                                     |
+----------------------------------------+
1 行于数据集 (0.01 秒)

2.2.9、trim()

TRIM(str): 返回字符串 str,所有前缀或后缀被删除了。

mysql> select trim('hello world');
+-------------------------+
| trim('hello world') |
+-------------------------+
| hello world             |
+-------------------------+
1 行于数据集 (0.01 秒)

2.2.10、ltrim()/rtrim()

LTRIM(str): 返回删除了其前置空格字符的字符串 str。

RTRIM(str): 返回删除了其拖后空格字符的字符串 str。

mysql> select ltrim('hello world');
+--------------------------+
| ltrim('hello world') |
+--------------------------+
| hello world              |
+--------------------------+
1 行于数据集 (0.01 秒)

mysql> select rtrim('hello world');
+--------------------------+
| rtrim('hello world') |
+--------------------------+
|   hello world            |
+--------------------------+
1 行于数据集 (0.01 秒)

2.2.11、replace()

REPLACE(str,from_str,to_str): 返回字符串 str,其字符串 from_str 的所有出现由字符串 to_str 代替。

mysql> select replace('hello world','o','O');
+--------------------------------+
| replace('hello world','o','O') |
+--------------------------------+
| hellO wOrld                    |
+--------------------------------+
1 行于数据集 (0.01 秒)

2.3、常用字符串函数

2.3.1、repeat()

REPEAT(str,count): 返回由重复 count 次的字符串 str 组成的一个字符串。如果 count <= 0,返回一个空字符串。如果 str 或 count 是 NULL,返回 NULL。

mysql> select repeat('hello',3);
+-------------------+
| repeat('hello',3) |
+-------------------+
| hellohellohello   |
+-------------------+
1 行于数据集 (0.01 秒)

mysql> select repeat('hello',0);
+-------------------+
| repeat('hello',0) |
+-------------------+
|                   |
+-------------------+
1 行于数据集 (0.01 秒)

2.3.2、reverse()

REVERSE(str): 返回颠倒字符顺序的字符串 str。

mysql> select reverse('hello world!');
+-------------------------+
| reverse('hello world!') |
+-------------------------+
| !dlrow olleh            |
+-------------------------+
1 行于数据集 (0.02 秒)

2.3.3、insert()

INSERT(str,pos,len,newstr): 返回字符串 str,在位置 pos 起始的子串且 len 个字符长的子串由字符串 newstr 代替。

mysql> select insert('hello world!',5,3,'is');
+---------------------------------+
| insert('hello world!',5,3,'is') |
+---------------------------------+
| hellisorld!                     |
+---------------------------------+
1 行于数据集 (0.02 秒)

2.3.4、elt()

ETL(index,str1,str2,str3…): 返回指定 index 位置的字符串

mysql> select elt(2,'hello','world','!');
+----------------------------+
| elt(2,'hello','world','!') |
+----------------------------+
| world                      |
+----------------------------+
1 行于数据集 (0.01 秒)

mysql> select elt(4,'hello','world','!');
+----------------------------+
| elt(4,'hello','world','!') |
+----------------------------+
| NULL                       |
+----------------------------+
1 行于数据集 (0.01 秒)

2.3.5、upper()

UPPER(x) 或 UCASE(x): 用于将字母转成大写,x 可以是单个字母也可以是字符串;

mysql> select upper('abcdfe');
+-----------------+
| upper('abcdfe') |
+-----------------+
| ABCDFE          |
+-----------------+
1 行于数据集 (0.01 秒)

mysql> select ucase('abcdfe');
+-----------------+
| ucase('abcdfe') |
+-----------------+
| ABCDFE          |
+-----------------+
1 行于数据集 (0.01 秒)

2.3.6、lower()

LOWER(x) 或 LCASE(x): 用于将字母转成小写,x 可以是单个字母也可以是字符串;

mysql> select lower('ABCDEF');
+-----------------+
| lower('ABCDEF') |
+-----------------+
| abcdef          |
+-----------------+
1 行于数据集 (0.01 秒)

mysql> select lcase('ABCDEF');
+-----------------+
| lcase('ABCDEF') |
+-----------------+
| abcdef          |
+-----------------+
1 行于数据集 (0.01 秒)

2.3.7、char_length()

CHAR_LENGTH(): 获取字符串字符数,获取字符串长度

mysql> select char_length('hello world');
+----------------------------+
| char_length('hello world') |
+----------------------------+
| 11                         |
+----------------------------+
1 行于数据集 (0.01 秒)

mysql> select char_length('你好');
+-------------------+
| char_length('你好') |
+-------------------+
| 2                 |
+-------------------+
1 行于数据集 (0.01 秒)

注意:

不管汉字还是数字或者是字母都算是一个字符

2.3.8、strcmp()

STRCMP(str1,str2): 比较两个字符串的大小。左边大于右边时返回 1,左边等于右边时返回 0,,左小于于右时返回 -1。

mysql> select strcmp('a','b');
+-----------------+
| strcmp('a','b') |
+-----------------+
| -1              |
+-----------------+
1 行于数据集 (0.01 秒)

mysql> select strcmp('d','b');
+-----------------+
| strcmp('d','b') |
+-----------------+
| 1               |
+-----------------+
1 行于数据集 (0.01 秒)

mysql> select strcmp('b','b');
+-----------------+
| strcmp('b','b') |
+-----------------+
| 0               |
+-----------------+
1 行于数据集 (0.01 秒)

2.3.9、field()

FIELD(str,str1,str2,str3…): 与 find_in_set 类似,返回 str 在 str1,str2,str3…中的位置。

mysql> select field('a','b','c','d','a','e');
+--------------------------------+
| field('a','b','c','d','a','e') |
+--------------------------------+
| 4                              |
+--------------------------------+
1 行于数据集 (0.02 秒)

mysql> select find_in_set('a','b,c,d,a,e');
+------------------------------+
| find_in_set('a','b,c,d,a,e') |
+------------------------------+
| 4                            |
+------------------------------+
1 行于数据集 (0.02 秒)

2.3.10、position()

POSITION(str1 IN str2): 返回子串 str1 在字符串 str2 中的位置

mysql> select position('ld' in 'helloworld');
+--------------------------------+
| position('ld' in 'helloworld') |
+--------------------------------+
| 9                              |
+--------------------------------+
1 行于数据集 (0.01 秒)

2.3.11、locate()

LOCATE(str1,str,pos): 函数返回字符串 str1 在 str 中的第 pos 位置后第一次出现的位置。如果 str1 在 str 中不存在,返回 0。

mysql> select locate('hel','hello world',1);
+-------------------------------+
| locate('hel','hello world',1) |
+-------------------------------+
| 1                             |
+-------------------------------+
1 行于数据集 (0.01 秒)

mysql> select locate('hel','hello world',2);
+-------------------------------+
| locate('hel','hello world',2) |
+-------------------------------+
| 0                             |
+-------------------------------+
1 行于数据集 (0.01 秒)

2.3.12、substring_index()

SUBSTRING_INDEX(str,delim,count): 在定界符 delim 及 count 出现前,从字符串 str 返回自字符串。若 count 为正值, 则返回最终定界符 (从左边开始),若为 - 1 则是从后往前截取;

mysql> select substring_index('hello/world/!','/',-1);
+-----------------------------------------+
| substring_index('hello/world/!','/',-1) |
+-----------------------------------------+
| !                                       |
+-----------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select substring_index('hello/world/!','/',1);
+----------------------------------------+
| substring_index('hello/world/!','/',1) |
+----------------------------------------+
| hello                                  |
+----------------------------------------+
1 行于数据集 (0.01 秒)

三、日期和时间函数

3.1、日期和时间函数概述

日期和时间函数是 MySQL 中常用的一类函数。主要用于处理日期时间。

3.2、常用日期和时间函数

3.2.1、curdate()

CURDATE() 或 CURRENT_DATE(): 返回当前日期

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2020-02-17 |
+------------+
1 行于数据集 (0.01 秒)

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2020-02-17     |
+----------------+
1 行于数据集 (0.01 秒)

3.2.2、curtime()

CURTIME() 或 CURRENT_TIME():返回当前时间

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 03:01:16  |
+-----------+
1 行于数据集 (0.01 秒)

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 03:01:26       |
+----------------+
1 行于数据集 (0.01 秒)

3.2.3、now()

NOW(): 返回当前日期时间

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-02-17 03:02:09 |
+---------------------+
1 行于数据集 (0.01 秒)

3.2.4、month()

MONTH(date) 或 MONTHNAME(date): 返回 date 的月份,范围 1 到 12

mysql> select month(now());
+--------------+
| month(now()) |
+--------------+
| 2            |
+--------------+
1 行于数据集 (0.01 秒)

mysql> select monthname(now());
+------------------+
| monthname(now()) |
+------------------+
| February         |
+------------------+
1 行于数据集 (0.01 秒)

3.2.5、week()

WEEK(date): 从日期中选择出周数

mysql> select week(now());
+-------------+
| week(now()) |
+-------------+
| 7           |
+-------------+
1 行于数据集 (0.01 秒)

3.2.6、year()

YEAR(date): 从日期中选择出年份

mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2020        |
+-------------+
1 行于数据集 (0.01 秒)

3.2.7、hour()

HOUR(date): 从日期中选择出小时数, 返回 time 的小时,范围是 0 到 23。

mysql> select hour(now());
+-------------+
| hour(now()) |
+-------------+
| 3           |
+-------------+
1 行于数据集 (0.02 秒)

3.2.8、minute()

MINUTE(): 从日期中选择出分钟数, 范围是 0 到 59。

mysql> select minute(now());
+---------------+
| minute(now()) |
+---------------+
| 12            |
+---------------+
1 行于数据集 (0.01 秒)

3.2.9、second()

SECOND(time): 回来 time 的秒数,范围是 0 到 59。

mysql> select second(now());
+---------------+
| second(now()) |
+---------------+
| 41            |
+---------------+
1 行于数据集 (0.01 秒)

3.3、常用日期和时间函数

3.3.1、weekday()

WEEKDAY(date) 或 DAYNAME(date): 返回 date 的星期索引 (0= 星期一,1= 星期二, ……6= 星期天)。

mysql> select weekday(now());
+----------------+
| weekday(now()) |
+----------------+
| 0              |
+----------------+
1 行于数据集 (0.01 秒)

mysql> select dayname(now());
+----------------+
| dayname(now()) |
+----------------+
| Monday         |
+----------------+
1 行于数据集 (0.01 秒)

3.3.2、dayofweek()

DAYOFWEEK(date): 返回日期 date 的星期索引 (1= 星期天,2= 星期一, …7= 星期六)。

mysql> select dayofweek(now());
+------------------+
| dayofweek(now()) |
+------------------+
| 2                |
+------------------+
1 行于数据集 (0.01 秒)

3.3.3、dayofmonth()

DAYOFMONTH(date): 返回 date 的月份中的日期,在 1 到 31 范围内。

mysql> select dayofmonth(now());
+-------------------+
| dayofmonth(now()) |
+-------------------+
| 17                |
+-------------------+
1 行于数据集 (0.01 秒)

3.3.4、dayofyear()

DAYOFYEAR(date): 返回 date 在一年中的日数, 在 1 到 366 范围内。

mysql> select dayofyear(now());
+------------------+
| dayofyear(now()) |
+------------------+
| 48               |
+------------------+
1 行于数据集 (0.01 秒)

3.3.5、quarter()

QUARTER(date): 返回 date 一年中的季度,范围 1 到 4。

mysql> select quarter(now());
+----------------+
| quarter(now()) |
+----------------+
| 1              |
+----------------+
1 行于数据集 (0.02 秒)

3.3.6、date_add()

DATE_ADD(date,INTERVAL expr type) , 进行日期增加的操作,可以精确到秒

mysql> select '1997-12-31 23:59:59'+interval 1 second;
+-----------------------------------------+
| '1997-12-31 23:59:59'+interval 1 second |
+-----------------------------------------+
| 1998-01-01 00:00:00                     |
+-----------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select '1997-12-31'+interval 1 second;
+--------------------------------+
| '1997-12-31'+interval 1 second |
+--------------------------------+
| 1997-12-31 00:00:01            |
+--------------------------------+
1 行于数据集 (0.02 秒)
mysql> select date_add('1997-12-31 23:59:59',interval 1 second);
+---------------------------------------------------+
| date_add('1997-12-31 23:59:59',interval 1 second) |
+---------------------------------------------------+
| 1998-01-01 00:00:00                               |
+---------------------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select date_add('1997-12-31 23:59:59',interval '1:1' minute_second);
+--------------------------------------------------------------+
| date_add('1997-12-31 23:59:59',interval '1:1' minute_second) |
+--------------------------------------------------------------+
| 1998-01-01 00:01:00                                          |
+--------------------------------------------------------------+
1 行于数据集 (0.01 秒)

3.3.7、date_sub()

DATE_SUB(date,INTERVAL expr type),进行日期减少的操作,可以精确到秒

mysql> select '1997-12-31'-interval 1 second;
+--------------------------------+
| '1997-12-31'-interval 1 second |
+--------------------------------+
| 1997-12-30 23:59:59            |
+--------------------------------+
1 行于数据集 (0.01 秒)
mysql> select date_sub('1997-12-31 23:59:59',interval '1:1' minute_second);
+--------------------------------------------------------------+
| date_sub('1997-12-31 23:59:59',interval '1:1' minute_second) |
+--------------------------------------------------------------+
| 1997-12-31 23:58:58                                          |
+--------------------------------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select date_sub('1997-12-31 23:59:59',interval 30 day);
+-------------------------------------------------+
| date_sub('1997-12-31 23:59:59',interval 30 day) |
+-------------------------------------------------+
| 1997-12-01 23:59:59                             |
+-------------------------------------------------+
1 行于数据集 (0.01 秒)

四、系统信息函数

4.1、系统函数概述

系统信息函数用来查询 MySQL 数据库的系统信息。

4.2、常用系统函数

4.2.1、version()

VERSION() 函数返回数据库的版本号;

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.17    |
+-----------+
1 行于数据集 (0.01 秒)

4.2.2、connection_id()

CONNECTION_ID() 函数返回服务器的连接数,也就是到现在为止 MySQL 服务的连接次数;

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 78              |
+-----------------+
1 行于数据集 (0.01 秒)

4.2.3、database()

DATABASE() 和 SCHEMA() 返回当前数据库名。

mysql> select database();
+------------+
| database() |
+------------+
| zutuanxue   |
+------------+
1 行于数据集 (0.01 秒)

mysql> select schema();
+----------+
| schema() |
+----------+
| zutuanxue |
+----------+
1 行于数据集 (0.01 秒)

4.2.4、用户函数

USER()、SYSTEM_USER()、SESSION_USER()、CURRENT_USER() 和 CURRENT_USER 这几个函数可以返回当前用户的名称。

mysql> select user();
+--------------------+
| user()             |
+--------------------+
| root@192.168.1.104 |
+--------------------+
1 行于数据集 (0.01 秒)

mysql> select system_user();
+--------------------+
| system_user()      |
+--------------------+
| root@192.168.1.104 |
+--------------------+
1 行于数据集 (0.01 秒)

mysql> select session_user();
+--------------------+
| session_user()     |
+--------------------+
| root@192.168.1.104 |
+--------------------+
1 行于数据集 (0.02 秒)

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@%         |
+----------------+
1 行于数据集 (0.01 秒)

mysql> select current_user;
+--------------+
| current_user |
+--------------+
| root@%       |
+--------------+
1 行于数据集 (0.01 秒)

4.2.5、字符集函数

charset()

CHARSET(str) 函数返回字符串 str 的字符集,一般情况这个字符集就是系统的默认字符集;

mysql> select charset('ad');
+---------------+
| charset('ad') |
+---------------+
| utf8          |
+---------------+
1 行于数据集 (0.01 秒)

collation()

COLLATION(str) 函数返回字符串 str 的字符排列方式。

mysql> select collation('ad');
+-----------------+
| collation('ad') |
+-----------------+
| utf8_general_ci |
+-----------------+
1 行于数据集 (0.01 秒)

五、其他函数

5.1、md5()

加密函数是 MySQL 中用来对数据进行加密的函数。

MD5(str) 函数可以对字符串 str 进行加密。MD5(str) 函数主要对普通的数据进行加密。下面使用 MD5(str) 函数为字符串“abcd”加密。

mysql> select md5('abcd');
+----------------------------------+
| md5('abcd')                      |
+----------------------------------+
| e2fc714c4727ee9395f324cd2e7f331f |
+----------------------------------+
1 行于数据集 (0.04 秒)

5.2、format()

格式化函数 FORMAT(x,n)

FORMAT(x,n) 函数可以将数字 x 进行格式化,将 x 保留到小数点后 n 位。这个过程需要进行四舍五入。例如 FORMAT(2.356,2) 返回的结果将会是 2.36;FORMAT(2.353,2) 返回的结果将会是 2.35。下面使用 FORMAT(x,n) 函数来讲 235.3456 和 235.3454 进行格式化,都保留到小数点后 3 位。

mysql> select format(235.3456,3),format(235.3456,2);
+--------------------+--------------------+
| format(235.3456,3) | format(235.3456,2) |
+--------------------+--------------------+
| 235.346            | 235.35             |
+--------------------+--------------------+
1 行于数据集 (0.01 秒)

5.3、进制转换函数

BIN(x) 返回 x 的二进制编码;

HEX(x) 返回 x 的十六进制编码;

OCT(x) 返回 x 的八进制编码;

CONV(x,f1,f2) 将 x 从 f1 进制数变成 f2 进制数。

mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010    |
+---------+
1 行于数据集 (0.01 秒)

mysql> select hex(10);
+---------+
| hex(10) |
+---------+
| A       |
+---------+
1 行于数据集 (0.01 秒)

mysql> select oct(10);
+---------+
| oct(10) |
+---------+
| 12      |
+---------+
1 行于数据集 (0.01 秒)

mysql> select conv(10,10,2);
+---------------+
| conv(10,10,2) |
+---------------+
| 1010          |
+---------------+
1 行于数据集 (0.01 秒)

5.4、条件判断函数

5.4.1、if()

IF(expr,v1,v2) 如果表达式 expr 成立,返回结果 v1;否则,返回结果 v2

mysql> select if(1>0,'yes','no');
+--------------------+
| if(1>0,'yes','no') |
+--------------------+
| yes                |
+--------------------+
1 行于数据集 (0.01 秒)

mysql> select if(strcmp('test','test1'),'yes','no');
+---------------------------------------+
| if(strcmp('test','test1'),'yes','no') |
+---------------------------------------+
| yes                                   |
+---------------------------------------+
1 行于数据集 (0.01 秒)

5.4.2、ifnull()

IFNULL(v1,v2): 如果 v1 不为 NULL,则返回 v1,否则返回 v2

mysql> select ifnull('yes','no');
+--------------------+
| ifnull('yes','no') |
+--------------------+
| yes                |
+--------------------+
1 行于数据集 (0.02 秒)

mysql> select ifnull(null,'no');
+-------------------+
| ifnull(null,'no') |
+-------------------+
| no                |
+-------------------+
1 行于数据集 (0.01 秒)

5.4.3、case

CASE expr WHEN v1 THEN r1 [WHEN v2 THEN v2] [ELSE rn] END: 如果 expr 等于某个 vn,则返回对应位置 THEN 后面的结果,如果与所有值都不想等,则返回 ELSE 后面的 rn

mysql> select case 11 when 1 then 'one' when 2 then 'two' else 'more' end;
+-------------------------------------------------------------+
| case 11 when 1 then 'one' when 2 then 'two' else 'more' end |
+-------------------------------------------------------------+
| more                                                        |
+-------------------------------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select case when 1>0 then 'true' else 'false' end;
+--------------------------------------------+
| case when 1>0 then 'true' else 'false' end |
+--------------------------------------------+
| true                                       |
+--------------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select case binary 'B' when 'a' then 1 when 'b' then 2 end;
+-----------------------------------------------------+
| case binary 'B' when 'a' then 1 when 'b' then 2 end |
+-----------------------------------------------------+
| NULL                                                |
+-----------------------------------------------------+
1 行于数据集 (0.01 秒)

mysql> select case binary 'B' when 'a' then 1 when 'B' then 2 end;
+-----------------------------------------------------+
| case binary 'B' when 'a' then 1 when 'B' then 2 end |
+-----------------------------------------------------+
| 2                                                   |
+-----------------------------------------------------+
1 行于数据集 (0.01 秒)

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-09发表,共计21947字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中