MySQL内置函数

文章目录

    • [1. 日期函数](#1. 日期函数)
    • [2. 字符串函数](#2. 字符串函数)
    • [3. 数学函数](#3. 数学函数)
    • [4. 其他函数](#4. 其他函数)

1. 日期函数

函数名称 说明
current_date() 当前日期(年月日)
current_time() 当前时间(时分秒)
current_timestamp() 当前时间戳
now() 当前时间日期
date(datetime) 返回datetime参数的日期部分
date_add(date, interval d_value_type) date中添加的日期或时间 interval数值单位可以是year、day、minute、second
date_sub(date, interval d_value_type) date中减去的日期或时间 interval数值单位可以是year、day、minute、second
datediff(date1, date2) 两个日期的差,单位是天
mysql 复制代码
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-05-07     |
+----------------+
1 row in set (0.00 sec)

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 14:03:20       |
+----------------+
1 row in set (0.00 sec)

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-05-07 14:03:27 |
+---------------------+
1 row in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-05-07 14:03:39 |
+---------------------+
1 row in set (0.00 sec)

mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2024-05-07  |
+-------------+
1 row in set (0.00 sec)

mysql> select date_add(now(), interval 20 day);
+----------------------------------+
| date_add(now(), interval 20 day) |
+----------------------------------+
| 2024-05-27 14:04:49              |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select date_sub(now(), interval 8 day);
+---------------------------------+
| date_sub(now(), interval 8 day) |
+---------------------------------+
| 2024-04-29 14:05:09             |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select datediff(date(now()), '2000-10-24');
+-------------------------------------+
| datediff(date(now()), '2000-10-24') |
+-------------------------------------+
|                                8596 |
+-------------------------------------+
1 row in set (0.00 sec)

++生日表:++

mysql 复制代码
mysql> create table birthday_table (
    -> id int primary key auto_increment,
    -> name varchar(20),
    -> birthday date
    -> );
mysql 复制代码
mysql> insert into birthday_table (name,birthday) values ('wjt', '2000-10-24');
mysql> insert into birthday_table (name,birthday) values ('china', '1949-10-1');
mysql> insert into birthday_table (name,birthday) values ('baby',current_date());
mysql> insert into birthday_table (name,birthday) values ('baby',current_time());

这里发现不管是插入时间还是日期,mysql会自动转换为日期,但建议还是按照要求插入

++留言表:++

mysql 复制代码
mysql> create table msg (
    -> id int primary key auto_increment,
    -> content varchar(100) not null,
    -> sendtime datetime
    -> );
mysql 复制代码
mysql> insert into msg (content, sendtime) values ('我猜今天很热, 因为路上行人的手臂和腿都露在外面', now());
mysql> insert into msg (content, sendtime) values ('不用猜,今天29度', now());
mysql> insert into msg (content, sendtime) values ('好的,我知道了,我也要把手臂和腿露在外面', now());

查询3分钟内的留言:

mysql 复制代码
mysql> select content, sendtime from msg where sendtime > date_sub(now(), interval 3 minute);

2. 字符串函数

函数 说明
charset(str) 返回字符串字符集
concat(str1, str2, ...) 连接字符串
instr(str, substring) 返回substring在str中出现的位置,没有返回0
ucase(str) 转成大写
lcase(str) 转成小写
left(str, length) right(str, length) 从str左/右边起,去length个字符
length(str) str长度
replace(str, from_str, to_str) 将str中from_str的内容替换成to_str
strcmp(str1, str2) 逐字符比较两个字符串大小
substring(str, position [, length]) 从str的position位置开始,去length个字符
ltrim(str) rtrim(str) trim(str) 去除前空格/后空格

++基本示例:++

mysql 复制代码
mysql> select charset('abcd');
+-----------------+
| charset('abcd') |
+-----------------+
| utf8            |
+-----------------+
1 row in set (0.00 sec)

mysql> select charset('武汉');
+-------------------+
| charset('武汉')   |
+-------------------+
| utf8              |
+-------------------+
1 row in set (0.00 sec)

mysql> select concat('aaa', 'bbb');
+----------------------+
| concat('aaa', 'bbb') |
+----------------------+
| aaabbb               |
+----------------------+
1 row in set (0.00 sec)

mysql> select instr('12345abc67', 'abc');
+----------------------------+
| instr('12345abc67', 'abc') |
+----------------------------+
|                          6 |
+----------------------------+
1 row in set (0.00 sec)

mysql> select ucase('mysql123');
+-------------------+
| ucase('mysql123') |
+-------------------+
| MYSQL123          |
+-------------------+
1 row in set (0.00 sec)

mysql> select lcase('456TTFTa');
+-------------------+
| lcase('456TTFTa') |
+-------------------+
| 456ttfta          |
+-------------------+
1 row in set (0.00 sec)

mysql> select left('1234qwer', 5);
+---------------------+
| left('1234qwer', 5) |
+---------------------+
| 1234q               |
+---------------------+
1 row in set (0.00 sec)

mysql> select right('1234qwer', 5);
+----------------------+
| right('1234qwer', 5) |
+----------------------+
| 4qwer                |
+----------------------+
1 row in set (0.00 sec)

mysql> select length('hello123');
+--------------------+
| length('hello123') |
+--------------------+
|                  8 |
+--------------------+
1 row in set (0.00 sec)

++获取某列的字符集:++

mysql 复制代码
mysql> select charset(sal) from emp;
mysql> select charset(ename) from emp;

表中出现乱码时,可查看该列字符集哪里有问题

++指定格式显示成绩:++

mysql 复制代码
mysql> select concat('姓名: ', name, ' 语文:', chinese, ' 数学:', math, ' 英语:', english, ' 总分:', chinese+math+english) 成绩单 from exam_ret;

++求学生名字占用字节数:++

mysql 复制代码
mysql> select name, length(name) from exam_ret;

utf8编码,一个汉字占3个字节

++子串替换:++

mysql 复制代码
mysql> select replace(name, '张', 'zhang') from exam_ret;

这里只是字符串级别的更换,并不更改表中的数据

3. 数学函数

函数 说明
abs(number) 绝对值
bin(decimal_number) 十进制转二进制
hex(decimalNumber) 转十六进制
conv(number, from_base, to_base) 进制转换
ceiling(number) 向上取整
floor(number) 向下取整
format(number, decimal_places) 格式化,保留小数位数
rand() 返回随机浮点数,范围[0.0, 1.0]
mod(number, denminator) 取模,求余

++基本示例:++

mysql 复制代码
mysql> select abs(-100);
+-----------+
| abs(-100) |
+-----------+
|       100 |
+-----------+
1 row in set (0.00 sec)

mysql> select bin(15);
+---------+
| bin(15) |
+---------+
| 1111    |
+---------+
1 row in set (0.00 sec)

mysql> select bin(3.14);
+-----------+
| bin(3.14) |
+-----------+
| 11        |
+-----------+
1 row in set (0.00 sec)

mysql> select hex(12);
+---------+
| hex(12) |
+---------+
| C       |
+---------+
1 row in set (0.00 sec)

mysql> select conv(12, 10, 2);
+-----------------+
| conv(12, 10, 2) |
+-----------------+
| 1100            |
+-----------------+
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)

mysql> select mod(5,2);
+----------+
| mod(5,2) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.3376762814239378 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.24748006765495992 |
+---------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*50, 0);
+----------------------+
| format(rand()*50, 0) |
+----------------------+
| 11                   |
+----------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*50, 0);
+----------------------+
| format(rand()*50, 0) |
+----------------------+
| 19                   |
+----------------------+
1 row in set (0.00 sec)
mysql> select ceiling(3.1);
+--------------+
| ceiling(3.1) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

mysql> select ceiling(3.8);
+--------------+
| ceiling(3.8) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

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

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

关于向上向下取整:

4. 其他函数

++user()查询当前用户:++

mysql 复制代码
mysql> select user();
+--------+
| user() |
+--------+
| root@  |
+--------+

++database()查询当前所在数据库:++

mysql 复制代码
mysql> select database();
+------------+
| database() |
+------------+
| test_db    |
+------------+

++md5(str)对字符串进行MD5摘要++

mysql 复制代码
mysql> select md5('123qwe');
+----------------------------------+
| md5('123qwe')                    |
+----------------------------------+
| 46f94c8de14fb36680850768ff1b7f2a |
+----------------------------------+

++password()对数据进行加密++

mysql 复制代码
mysql> select password('hello_123');
+-------------------------------------------+
| password('hello_123')                     |
+-------------------------------------------+
| *CD543CEE05473FCBF9D5082022E5DA52362350C3 |
+-------------------------------------------+

++ifnull(val1, val2)val1为空,返回val2的值,否则返回val1++

mysql 复制代码
mysql> select ifnull(null,null) reslut;
+--------+
| reslut |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

mysql> select ifnull(null,3) reslut;
+--------+
| reslut |
+--------+
|      3 |
+--------+
1 row in set (0.00 sec)
相关推荐
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099403 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
内核程序员kevin3 小时前
TCP Listen 队列详解与优化指南
linux·网络·tcp/ip
Dlwyz3 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
工业甲酰苯胺5 小时前
Redis性能优化的18招
数据库·redis·性能优化
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
i道i6 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
小怪兽ysl6 小时前
【PostgreSQL使用pg_filedump工具解析数据文件以恢复数据】
数据库·postgresql
wqq_9922502777 小时前
springboot基于微信小程序的食堂预约点餐系统
数据库·微信小程序·小程序
爱上口袋的天空7 小时前
09 - Clickhouse的SQL操作
数据库·sql·clickhouse