MySQL 内置函数

1. 日期函数

1.1 部分介绍

函数名 描述
CURRENT_DATE() 返回当前日期
CURRENT_TIME 返回当前时间
CURRENT_TIMESTAMP() 返回当前日期和时间
DATEDIFF(d1,d2) 计算日期 d1->d2 之间相隔的天数
DATE_ADD(d,INTERVAL expr type) 计算起始日期 d 加上一个时间段后的日期,type 值可以是: year, minute, second, hour, day, week...
DATE_SUB(date,INTERVAL expr type) 函数从日期减去指定的时间间隔。

这里仅列出了部分,详情见链接

1.2 获得当前日期,时间,时间戳,日期时间

sql 复制代码
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-11-24     |
+----------------+
1 row in set (0.07 sec)

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 22:54:42       |
+----------------+
1 row in set (0.08 sec)

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-11-24 22:55:02 |
+---------------------+
1 row in set (0.07 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-11-24 22:56:48 |
+---------------------+
1 row in set (0.10 sec)

1.2 返回datetime的日期部分

sql 复制代码
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2024-11-24  |
+-------------+
1 row in set (0.10 sec)

mysql> select date(current_timestamp());
+---------------------------+
| date(current_timestamp()) |
+---------------------------+
| 2024-11-24                |
+---------------------------+
1 row in set (0.09 sec)

mysql> select date("2004-04-24 00:00:00");
+-----------------------------+
| date("2004-04-24 00:00:00") |
+-----------------------------+
| 2004-04-24                  |
+-----------------------------+
1 row in set (0.10 sec)

mysql> select date("2004-04-24 hhhxxxxxxxxxx");
+----------------------------------+
| date("2004-04-24 hhhxxxxxxxxxx") |
+----------------------------------+
| 2004-04-24                       |
+----------------------------------+
1 row in set (0.08 sec)

1.3 日期加减,日期的差

sql 复制代码
mysql> select date_add(current_date(), interval 1000 day);
+---------------------------------------------+
| date_add(current_date(), interval 1000 day) |
+---------------------------------------------+
| 2027-08-21                                  |
+---------------------------------------------+
1 row in set (0.09 sec)

mysql> select date_add(current_date(), interval 1000 year);
+----------------------------------------------+
| date_add(current_date(), interval 1000 year) |
+----------------------------------------------+
| 3024-11-24                                   |
+----------------------------------------------+
1 row in set (0.10 sec)

mysql> select date_sub(current_date(), interval 1000 month);
+-----------------------------------------------+
| date_sub(current_date(), interval 1000 month) |
+-----------------------------------------------+
| 1941-07-24                                    |
+-----------------------------------------------+
1 row in set (0.08 sec)

-- 拿前面的日期减后面的日期
mysql> select datediff(current_date(), '2004-04-25');
+----------------------------------------+
| datediff(current_date(), '2004-04-25') |
+----------------------------------------+
|                                   7518 |
+----------------------------------------+
1 row in set (0.11 sec)

mysql> select datediff('2004-04-25', current_date());
+----------------------------------------+
| datediff('2004-04-25', current_date()) |
+----------------------------------------+
|                                  -7518 |
+----------------------------------------+
1 row in set (0.09 sec)

1.4 示例1,创建一个留言板

创建留言板并插入数据

sql 复制代码
mysql> CREATE TABLE msg (id int PRIMARY KEY auto_increment, content VARCHAR(100), time DATETIME);

mysql> insert into msg (content, time) values("hello...", now());
Query OK, 1 row affected (0.03 sec)

mysql> insert into msg (content, time) values("world...", now());
Query OK, 1 row affected (0.03 sec)

mysql> select * from msg;
+----+----------+---------------------+
| id | content  | time                |
+----+----------+---------------------+
|  1 | hello... | 2024-11-25 20:33:09 |
|  2 | world... | 2024-11-25 20:33:18 |
+----+----------+---------------------+
2 rows in set (0.08 sec)

如果想查询10分钟内发布的帖子

sql 复制代码
mysql> select * from msg where date_add(time, interval 10 minute) > now();
+----+----------+---------------------+
| id | content  | time                |
+----+----------+---------------------+
|  1 | hello... | 2024-11-25 20:33:09 |
|  2 | world... | 2024-11-25 20:33:18 |
+----+----------+---------------------+
2 rows in set (0.09 sec)

mysql> select * from msg where date_sub(now(), interval 10 minute) < time;
+----+----------+---------------------+
| id | content  | time                |
+----+----------+---------------------+
|  1 | hello... | 2024-11-25 20:33:09 |
|  2 | world... | 2024-11-25 20:33:18 |
+----+----------+---------------------+
2 rows in set (0.10 sec)

2. 字符串函数

1.1 部分介绍

函数 描述
CONCAT(s1,s2...sn) 字符串 s1,s2 等多个字符串合并为一个字符串
INSTR(s1, s2) 返回s2在s1中的位置,没有返回0,类似strstr()
UCASE(s) 将字符串转换为大写
UPPER(s) 将字符串转换为大写
LEFT(s,n) 返回字符串 s 的前 n 个字符
RIGHT(s,n) 返回字符串 s 的后 n 个字符
LENGTH(s) 返回s字符串的长度,返回的是字节数
REPLACE(s,s1,s2) 将字符串 s2 替代字符串 s 中的字符串 s1
STRCMP(s1,s2) 比较字符串 s1 和 s2,如果 s1 与 s2 相等返回 0 ,如果 s1>s2 返回 1,如果 s1<s2 返回 -1
SUBSTR(s, start, length) 从字符串 s 的 start 位置截取长度为 length 的子字符串
LTRIM(s) RTRIM(s) TRIM(s) 去掉字符串 s 开始处的空格。去除s结尾处的空格。去除开头和结尾的空格
CHARSET(s) 返回字符串 s 的字符集

1.2 简单使用

sql 复制代码
-- concat
mysql> select concat("aaa", "bbbbb", 1234, "cd123");
+---------------------------------------+
| concat("aaa", "bbbbb", 1234, "cd123") |
+---------------------------------------+
| aaabbbbb1234cd123                     |
+---------------------------------------+
1 row in set (0.09 sec)

-- instr
mysql> select instr("aaaaaaabbbbbbcccccc", "aaa");
+-------------------------------------+
| instr("aaaaaaabbbbbbcccccc", "aaa") |
+-------------------------------------+
|                                   1 |
+-------------------------------------+
1 row in set (0.06 sec)

mysql> select instr("aaaaaaabbbbbbcccccc", "bbb");
+-------------------------------------+
| instr("aaaaaaabbbbbbcccccc", "bbb") |
+-------------------------------------+
|                                   8 |
+-------------------------------------+
1 row in set (0.07 sec)

mysql> select instr("aaaaaaabbbbbbcccccc", "f");
+-----------------------------------+
| instr("aaaaaaabbbbbbcccccc", "f") |
+-----------------------------------+
|                                 0 |
+-----------------------------------+

-- ucase()和lcase()
mysql> select ucase("aaBBccDD123");
+----------------------+
| ucase("aaBBccDD123") |
+----------------------+
| AABBCCDD123          |
+----------------------+
1 row in set (0.08 sec)

mysql> select lcase("aaBBccDD123");
+----------------------+
| lcase("aaBBccDD123") |
+----------------------+
| aabbccdd123          |
+----------------------+
1 row in set (0.08 sec)

-- left 和 right
mysql> select left("aaBBccDD123", 4);
+------------------------+
| left("aaBBccDD123", 4) |
+------------------------+
| aaBB                   |
+------------------------+
1 row in set (0.07 sec)

mysql> select right("aaBBccDD123", 4);
+-------------------------+
| right("aaBBccDD123", 4) |
+-------------------------+
| D123                    |
+-------------------------+
1 row in set (0.08 sec)
-- 超过了就返回全部
mysql> select right("aaBBccDD123", 1111);
+----------------------------+
| right("aaBBccDD123", 1111) |
+----------------------------+
| aaBBccDD123                |
+----------------------------+
1 row in set (0.10 sec)

-- length()和trim(), utf-8编码 
mysql> select length('t');
+-------------+
| length('t') |
+-------------+
|           1 |
+-------------+
1 row in set (0.08 sec)

mysql> select length('它');
+--------------+
| length('它') |
+--------------+
|            3 |
+--------------+
1 row in set (0.10 sec)

mysql> select length(1);
+-----------+
| length(1) |
+-----------+
|         1 |
+-----------+
1 row in set (0.09 sec)

mysql> select trim("     aaBBccDD1 23   ");
+------------------------------+
| trim("     aaBBccDD1 23   ") |
+------------------------------+
| aaBBccDD1 23                 |
+------------------------------+
1 row in set (0.09 sec)

1.3 在数据库中使用

使用两个表,一个是emp表

一个是exam_result表

1.3.1 获取emp表的ename列的字符集 (charset)

sql 复制代码
mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
+----------------+
14 rows in set (0.12 sec)

-- 也可以查查别的列
mysql> select charset(sal) from emp;
+--------------+
| charset(sal) |
+--------------+
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
| binary       |
+--------------+
14 rows in set (0.12 sec)

1.3.2 要求显示exam_result表中的信息 (concat)

显示格式:"XXX的语文是XXX分,数学XXX分,英语XXX分, 总分XXX分"

sql 复制代码
mysql> select concat(name, "的语文是", chinese, "分, 数学", math, "分, 英语", english, "分, 总分: ", chinese+math+english, "分") msg from exam_result;
+-----------------------------------------------------+
| msg                                                 |
+-----------------------------------------------------+
| 唐三藏的语文是67分, 数学98分, 英语56分, 总分: 221分 |
| 孙悟空的语文是87分, 数学78分, 英语77分, 总分: 242分 |
| 猪悟能的语文是88分, 数学98分, 英语90分, 总分: 276分 |
| 曹孟德的语文是82分, 数学84分, 英语67分, 总分: 233分 |
| 刘玄德的语文是55分, 数学85分, 英语45分, 总分: 185分 |
| 孙权的语文是70分, 数学73分, 英语78分, 总分: 221分   |
| 宋公明的语文是75分, 数学65分, 英语30分, 总分: 170分 |
+-----------------------------------------------------+
7 rows in set (0.17 sec)

1.3.3 替换emp表信息 (replace)

将job列的ERK换成'职员'

sql 复制代码
mysql> select replace(job, "ERK", "职员") msg from emp;
+-----------+
| msg       |
+-----------+
| CL职员    |
| SALESMAN  |
| SALESMAN  |
| MANAGER   |
| SALESMAN  |
| MANAGER   |
| MANAGER   |
| ANALYST   |
| PRESIDENT |
| SALESMAN  |
| CL职员    |
| CL职员    |
| ANALYST   |
| CL职员    |
+-----------+
14 rows in set (0.12 sec)

1.3.4 截取emp表信息 (substring)

截取EMP表中ename字段的第二个到第三个字符

sql 复制代码
mysql> select ename from emp;
+--------+
| ename  |
+--------+
| SMITH  |
| ALLEN  |
| WARD   |
| JONES  |
| MARTIN |
| BLAKE  |
| CLARK  |
| SCOTT  |
| KING   |
| TURNER |
| ADAMS  |
| JAMES  |
| FORD   |
| MILLER |
+--------+
14 rows in set (0.12 sec)

mysql> select substring(ename, 2, 2) from emp;
+------------------------+
| substring(ename, 2, 2) |
+------------------------+
| MI                     |
| LL                     |
| AR                     |
| ON                     |
| AR                     |
| LA                     |
| LA                     |
| CO                     |
| IN                     |
| UR                     |
| DA                     |
| AM                     |
| OR                     |
| IL                     |
+------------------------+
14 rows in set (0.12 sec)

1.3.5 以首字母小写的方式显示所有员工的姓名

综合使用

sql 复制代码
mysql> select concat(lcase(left(ename, 1)), right(ename, length(ename)-1)) from emp;
+--------------------------------------------------------------+
| concat(lcase(left(ename, 1)), right(ename, length(ename)-1)) |
+--------------------------------------------------------------+
| sMITH                                                        |
| aLLEN                                                        |
| wARD                                                         |
| jONES                                                        |
| mARTIN                                                       |
| bLAKE                                                        |
| cLARK                                                        |
| sCOTT                                                        |
| kING                                                         |
| tURNER                                                       |
| aDAMS                                                        |
| jAMES                                                        |
| fORD                                                         |
| mILLER                                                       |
+--------------------------------------------------------------+

3. 数学函数

3.1 部分介绍

函数名 描述
ABS(x) 返回 x 的绝对值
BIN(decimal_num) 将十进制数转为2进制
HEX(decimal_num) 将十进制数转为16进制
CONV(num, fom_base, to_base) 将数字从from_base进制转为to_base进制
CEILING(x) 返回大于或等于 x 的最小整数(向上取整)
FLOOR(x) 返回小于或等于 x 的最大整数(向下取整)
RAND() 返回 0 到 1 的随机数
FORMAT(number, decimal_places) 格式化,保留小数位数
MOD(x,y) 返回 x 除以 y 以后的余数

3.2 基础使用

去绝对值,进制转换,取余

sql 复制代码
mysql> select abs(-11);
+----------+
| abs(-11) |
+----------+
|       11 |
+----------+
1 row in set (0.05 sec)

mysql> select bin(7);
+--------+
| bin(7) |
+--------+
| 111    |
+--------+
1 row in set (0.06 sec)

mysql> select hex(10);
+---------+
| hex(10) |
+---------+
| A       |
+---------+
1 row in set (0.06 sec)

mysql> select conv(8, 10, 8);
+----------------+
| conv(8, 10, 8) |
+----------------+
| 10             |
+----------------+
1 row in set (0.07 sec)

mysql> select mod(10, 4);
+------------+
| mod(10, 4) |
+------------+
|          2 |
+------------+
1 row in set (0.06 sec)

随机数,格式化

sql 复制代码
mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.04022560365519077 |
+---------------------+
1 row in set (0.07 sec)

mysql> select rand() * 100;
+-------------------+
| rand() * 100      |
+-------------------+
| 51.45658315295035 |
+-------------------+
1 row in set (0.09 sec)

mysql> select format(rand() * 100, 0);
+-------------------------+
| format(rand() * 100, 0) |
+-------------------------+
| 45                      |
+-------------------------+
1 row in set (0.09 sec)

mysql> select format(rand() * 100, 2);
+-------------------------+
| format(rand() * 100, 2) |
+-------------------------+
| 71.71                   |
+-------------------------+
1 row in set (0.09 sec)

向上,向下取整

sql 复制代码
mysql> select ceil(3.1);
+-----------+
| ceil(3.1) |
+-----------+
|         4 |
+-----------+
1 row in set (0.08 sec)

mysql> select ceil(-3.1);
+------------+
| ceil(-3.1) |
+------------+
|         -3 |
+------------+
1 row in set (0.10 sec)

mysql> select floor(3.1);
+------------+
| floor(3.1) |
+------------+
|          3 |
+------------+
1 row in set (0.08 sec)

mysql> select floor(-3.1);
+-------------+
| floor(-3.1) |
+-------------+
|          -4 |
+-------------+
1 row in set (0.10 sec)

4. 其它函数

user() 查询当前用户, database()查询当前用的数据库

sql 复制代码
mysql> select user();
+--------+
| user() |
+--------+
| root@  |
+--------+
1 row in set (0.06 sec)

mysql> select database();
+------------+
| database() |
+------------+
| testdb     |
+------------+
1 row in set (0.05 sec)

加密函数md5(str)password(str)

sql 复制代码
create table t1 (id int primary key auto_increment, password char(32));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into t1 (password) values ("aaaaBBaa11");
Query OK, 1 row affected (0.03 sec)

mysql> insert into t1 (password) values (md5("aaaaBBaa11"));
Query OK, 1 row affected (0.03 sec)

mysql> insert into t1 (password) values (password("aaaaBBaa11"));
Query OK, 1 row affected (0.03 sec)

mysql> select * from t1;
+----+-------------------------------------------+
| id | password                                  |
+----+-------------------------------------------+
|  1 | aaaaBBaa11                                |
|  2 | e574c3309a2fb2a0c239a22fcb4d6f70          |
|  3 | *F582F307B72CA2E12BB3A8B722A34AC223231182 |
+----+-------------------------------------------+

-- 如何查看当前用户的密码输入的正确还是不正确?
mysql> select * from t1 where id = 3 and password = (password('aaaaBBaa11'));
+----+-------------------------------------------+
| id | password                                  |
+----+-------------------------------------------+
|  3 | *F582F307B72CA2E12BB3A8B722A34AC223231182 |
+----+-------------------------------------------+
1 row in set (0.11 sec)

ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值

sql 复制代码
mysql> select ifnull(null, 123);
+-------------------+
| ifnull(null, 123) |
+-------------------+
|               123 |
+-------------------+
1 row in set (0.10 sec)

mysql> select ifnull("abc", 123);
+--------------------+
| ifnull("abc", 123) |
+--------------------+
| abc                |
+--------------------+
1 row in set (0.11 sec)
相关推荐
Mr_Xuhhh12 分钟前
程序地址空间
android·java·开发语言·数据库
大明湖的狗凯.21 分钟前
MySQL 中的乐观锁与悲观锁
java·数据库·mysql
真上帝的左手21 分钟前
数据库-MySQL-MybatisPlus整合多数据源
数据库·mysql·mybatis
奈斯ing21 分钟前
【MySQL篇】持久化和非持久化统计信息的深度剖析(第一篇,总共六篇)
数据库·mysql
真上帝的左手22 分钟前
数据库-MySQL-Mybatis源码解析-设计模式角度
数据库·mysql·mybatis
shiming88791 小时前
C/C++链接数据库(MySQL)超级详细指南
c语言·数据库·c++
前端白袍1 小时前
C语言:C语言实现对MySQL数据库表增删改查功能
c语言·数据库·mysql
Elastic 中国社区官方博客1 小时前
Elasticsearch:Retrievers 介绍
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
蓝桉柒71 小时前
安装数据库客户端工具
数据库
南城花随雪。2 小时前
Redis(非关系型数据库)详细介绍
数据库·redis·nosql