【MySQL】内置函数
日期函数
- 获取年月日
sql
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-08-12 |
+----------------+
1 row in set (0.01 sec)
- 获取时分秒
sql
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 13:53:48 |
+----------------+
1 row in set (0.00 sec)
- 获取时间戳
sql
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-08-12 13:54:15 |
+---------------------+
1 row in set (0.01 sec)
sql
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2024-08-12 13:54:46 |
+---------------------+
1 row in set (0.00 sec)
- 返回日期部分
sql
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2024-08-12 |
+-------------+
1 row in set (0.00 sec)
sql
mysql> select date(current_timestamp());
+---------------------------+
| date(current_timestamp()) |
+---------------------------+
| 2024-08-12 |
+---------------------------+
1 row in set (0.00 sec)
- 在日期的基础上加上日期
sql
mysql> select date_add('2024-10-1',interval 10 day);
+---------------------------------------+
| date_add('2024-10-1',interval 10 day) |
+---------------------------------------+
| 2024-10-11 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2024-10-1',interval 10 year);
+----------------------------------------+
| date_add('2024-10-1',interval 10 year) |
+----------------------------------------+
| 2034-10-01 |
+----------------------------------------+
1 row in set (0.00 sec)
- 在日期的基础上减上日期
sql
mysql> select date_sub('2024-10-1',interval 10 day);
+---------------------------------------+
| date_sub('2024-10-1',interval 10 day) |
+---------------------------------------+
| 2024-09-21 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2024-10-1',interval 10 year);
+----------------------------------------+
| date_sub('2024-10-1',interval 10 year) |
+----------------------------------------+
| 2014-10-01 |
+----------------------------------------+
1 row in set (0.01 sec)
- 计算连个日期的间隔天数
sql
mysql> select datediff(now(),'1949-10-1');
+-----------------------------+
| datediff(now(),'1949-10-1') |
+-----------------------------+
| 27344 |
+-----------------------------+
1 row in set (0.00 sec)
字符串函数
- charset查看使用的字符集
sql
mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8mb4 |
+----------------+
1 rows in set (0.00 sec)
- concat拼接字符串
sql
mysql> select concat('aaa','bbb',112);
+-------------------------+
| concat('aaa','bbb',112) |
+-------------------------+
| aaabbb112 |
+-------------------------+
1 row in set (0.01 sec)
mysql> select concat(name,'的总分:',chinese+math+english,',语文成绩:',chinese,',数学成绩:',math,',英语成绩:',english) as msg from exam;
+-------------------------------------------------------------------------+
| msg |
+-------------------------------------------------------------------------+
| 唐三藏的总分:288,语文成绩:134,数学成绩:98,英语成绩:56 |
| 猪悟能的总分:364,语文成绩:176,数学成绩:98,英语成绩:90 |
| 曹孟德的总分:297,语文成绩:140,数学成绩:90,英语成绩:67 |
| 孙权的总分:291,语文成绩:140,数学成绩:73,英语成绩:78 |
| 宋公明的总分:275,语文成绩:150,数学成绩:95,英语成绩:30 |
+-------------------------------------------------------------------------+
5 rows in set (0.00 sec)
- instr返回字符串所处的位置
sql
mysql> select instr('abcdefg123dg',123);
+---------------------------+
| instr('abcdefg123dg',123) |
+---------------------------+
| 8 |
+---------------------------+
1 row in set (0.00 sec)
- ucase/lcase大小写转换
sql
mysql> select ucase('abcd123ABCD');
+----------------------+
| ucase('abcd123ABCD') |
+----------------------+
| ABCD123ABCD |
+----------------------+
1 row in set (0.01 sec)
mysql> select lcase('abcd123ABCD');
+----------------------+
| lcase('abcd123ABCD') |
+----------------------+
| abcd123abcd |
+----------------------+
1 row in set (0.00 sec)
- 获取指定长度的字符串left/right
sql
mysql> select left('abcdefg',3);
+-------------------+
| left('abcdefg',3) |
+-------------------+
| abc |
+-------------------+
1 row in set (0.00 sec)
mysql> select right('abcdefg',3);
+--------------------+
| right('abcdefg',3) |
+--------------------+
| efg |
+--------------------+
1 row in set (0.00 sec)
- 查看字符串长度length
sql
mysql> select length('abcd');
+----------------+
| length('abcd') |
+----------------+
| 4 |
+----------------+
1 row in set (0.00 sec)
mysql> select name,length(name) from exam;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| 唐三藏 | 9 |
| 猪悟能 | 9 |
| 曹孟德 | 9 |
| 孙权 | 6 |
| 宋公明 | 9 |
+-----------+--------------+
5 rows in set (0.00 sec)
- 字符替换replace
将EMP表中所有名字中有S的替换成'上海'
sql
mysql> select ename,replace(ename,'S','上海') from emp;
+--------+-----------------------------+
| ename | replace(ename,'S','上海') |
+--------+-----------------------------+
| SMITH | 上海MITH |
| ALLEN | ALLEN |
| WARD | WARD |
| JONES | JONE上海 |
| MARTIN | MARTIN |
| BLAKE | BLAKE |
| CLARK | CLARK |
| SCOTT | 上海COTT |
| KING | KING |
| TURNER | TURNER |
| ADAMS | ADAM上海 |
| JAMES | JAME上海 |
| FORD | FORD |
| MILLER | MILLER |
+--------+-----------------------------+
14 rows in set (0.00 sec)
- 字符比较strcmp
sql
mysql> select strcmp('abc','abc');
+---------------------+
| strcmp('abc','abc') |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.01 sec)
mysql> select strcmp('abc','123');
+---------------------+
| strcmp('abc','123') |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)
# a.忽略大小写------跟字符集有关
mysql> select strcmp('abc','ABC');
+---------------------+
| strcmp('abc','ABC') |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.00 sec)
mysql> select strcmp('123','abc');
+---------------------+
| strcmp('123','abc') |
+---------------------+
| -1 |
+---------------------+
1 row in set (0.00 sec)
- 字符截取substring
截取EMP表中ename字段的第二个到第三个字符
sql
mysql> select ename,substring(ename,2,2) from emp;
+--------+----------------------+
| ename | substring(ename,2,2) |
+--------+----------------------+
| SMITH | MI |
| ALLEN | LL |
| WARD | AR |
| JONES | ON |
| MARTIN | AR |
| BLAKE | LA |
| CLARK | LA |
| SCOTT | CO |
| KING | IN |
| TURNER | UR |
| ADAMS | DA |
| JAMES | AM |
| FORD | OR |
| MILLER | IL |
+--------+----------------------+
14 rows in set (0.00 sec)
以首字母小写的方式显示所有员工的姓名
sql
mysql> select ename,concat(substring(ename,1,1),lcase(substring(ename,2))) as name from emp;
+--------+--------+
| ename | name |
+--------+--------+
| SMITH | Smith |
| ALLEN | Allen |
| WARD | Ward |
| JONES | Jones |
| MARTIN | Martin |
| BLAKE | Blake |
| CLARK | Clark |
| SCOTT | Scott |
| KING | King |
| TURNER | Turner |
| ADAMS | Adams |
| JAMES | James |
| FORD | Ford |
| MILLER | Miller |
+--------+--------+
14 rows in set (0.01 sec)
- 去除空格
sql
mysql> select ltrim(' mysql ') as ltrim;
+------------+
| ltrim |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
mysql> select rtrim(' mysql ') as rtrim;
+-----------+
| rtrim |
+-----------+
| mysql |
+-----------+
1 row in set (0.00 sec)
mysql> select trim(' mysql ') as trim;
+-------+
| trim |
+-------+
| mysql |
+-------+
1 row in set (0.00 sec)
数学函数
- abs取绝对值
sql
mysql> select abs(-1);
+---------+
| abs(-1) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
- 进制转换
sql
mysql> select bin(2);
+--------+
| bin(2) |
+--------+
| 10 |
+--------+
1 row in set (0.01 sec)
mysql> select hex(20);
+---------+
| hex(20) |
+---------+
| 14 |
+---------+
1 row in set (0.00 sec)
sql
mysql> select conv(10,10,2);
+---------------+
| conv(10,10,2) |
+---------------+
| 1010 |
+---------------+
1 row in set (0.00 sec)
- 向上,向下取整
sql
mysql> select ceiling(3.14);
+---------------+
| ceiling(3.14) |
+---------------+
| 4 |
+---------------+
1 row in set (0.00 sec)
mysql> select floor(3.14);
+-------------+
| floor(3.14) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)
mysql> select ceiling(-3.14);
+----------------+
| ceiling(-3.14) |
+----------------+
| -3 |
+----------------+
1 row in set (0.00 sec)
mysql> select floot(-3.14);
ERROR 1305 (42000): FUNCTION test_db.floot does not exist
mysql> select floor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
| -4 |
+--------------+
1 row in set (0.00 sec)
- 格式化,保留小数
sql
mysql> select format(3.1415926,2);
+---------------------+
| format(3.1415926,2) |
+---------------------+
| 3.14 |
+---------------------+
1 row in set (0.00 sec)
mysql> select format(3.1415926,4);
+---------------------+
| format(3.1415926,4) |
+---------------------+
| 3.1416 |
+---------------------+
1 row in set (0.00 sec)
- 随机值
sql
mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.39838555212913596 |
+---------------------+
1 row in set (0.00 sec)
mysql> select format(rand()*100,1);
+----------------------+
| format(rand()*100,1) |
+----------------------+
| 91.9 |
+----------------------+
1 row in set (0.02 sec)
- 取模求余
sql
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
mysql> select mod(10,-3);
+------------+
| mod(10,-3) |
+------------+
| 1 |
+------------+
1 row in set (0.00 sec)
mysql> select mod(-10,-3);
+-------------+
| mod(-10,-3) |
+-------------+
| -1 |
+-------------+
1 row in set (0.00 sec)
mysql> select mod(-10,3);
+------------+
| mod(-10,3) |
+------------+
| -1 |
+------------+
1 row in set (0.00 sec)
其他函数
- 查看当前用户user()
sql
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
- md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
再要我们在讲解https的时候有讲过
sql
mysql> select md5('admin');
+----------------------------------+
| md5('admin') |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
1 row in set (0.00 sec)
- database()显示当前正在使用的数据库
sql
mysql> select database();
+------------+
| database() |
+------------+
| test_db |
+------------+
1 row in set (0.00 sec)
- password()函数,MySQL数据库使用该函数对用户加密
MySQL 8.0版本中已经删除了PASSWORD()函数
sql
select password('root');
+-------------------------------------------+
| password('root') |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
- ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
这个有点向三木操作符
sql
mysql> select ifnull(null,'adb');
+--------------------+
| ifnull(null,'adb') |
+--------------------+
| adb |
+--------------------+
1 row in set (0.00 sec)
mysql> select ifnull('def',null);
+--------------------+
| ifnull('def',null) |
+--------------------+
| def |
+--------------------+
1 row in set (0.00 sec)
mysql> select ifnull('def','abd');
+---------------------+
| ifnull('def','abd') |
+---------------------+
| def |
+---------------------+
1 row in set (0.00 sec)