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)
相关推荐
watermelonoops35 分钟前
Deepin和Windows传文件(Xftp,WinSCP)
linux·ssh·deepin·winscp·xftp
云和数据.ChenGuang39 分钟前
Django 应用安装脚本 – 如何将应用添加到 INSTALLED_APPS 设置中 原创
数据库·django·sqlite
woshilys1 小时前
sql server 查询对象的修改时间
运维·数据库·sqlserver
Hacker_LaoYi1 小时前
SQL注入的那些面试题总结
数据库·sql
疯狂飙车的蜗牛2 小时前
从零玩转CanMV-K230(4)-小核Linux驱动开发参考
linux·运维·驱动开发
建投数据2 小时前
建投数据与腾讯云数据库TDSQL完成产品兼容性互认证
数据库·腾讯云
Hacker_LaoYi3 小时前
【渗透技术总结】SQL手工注入总结
数据库·sql
岁月变迁呀3 小时前
Redis梳理
数据库·redis·缓存
独行soc3 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw
你的微笑,乱了夏天4 小时前
linux centos 7 安装 mongodb7
数据库·mongodb