组团学

MySQL常用函数

阅读 (202456)

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 秒)
需要 登录 才可以提问哦