MySQL内置函数

目录

1.日期函数

1.1获得年月日

1.2获得时分秒

1.3获得年月日时分秒

1.4获得时间戳

1.5日期加天数

1.6日期减天数

1.7计算两个日期相隔多少天

1.8案例

2.字符串函数

2.1获取字符集

2.2字符串拼接

2.3字符串查询

2.4大小写转换

2.5字符串提取

2.6字符串长度

2.7字符串替换

2.8字符串截取

2.9去除空格

3.数学函数

3.1绝对值

3.2转二进制

3.3转十六进制

3.4进制转换

3.5向上向下取整

3.6保留n位小数

3.7随机数

3.8取模

4.其他函数

4.1查询当前用户

4.2显示当前数据库

4.3md5摘要

4.4passwor加密

4.5ifnull


1.日期函数

1.1获得年月日

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

1.2获得时分秒

sql 复制代码
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 22:10:04       |
+----------------+
1 row in set (0.00 sec)

1.3获得年月日时分秒

sql 复制代码
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-03-24 22:30:14 |
+---------------------+
1 row in set (0.00 sec)

1.4获得时间戳

会以日期的方式显示。

sql 复制代码
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-03-24 22:10:55 |
+---------------------+
1 row in set (0.00 sec)

1.5日期加天数

sql 复制代码
mysql> select date_add('2022-01-01', interval 12 day) as res;
+------------+
| res        |
+------------+
| 2022-01-13 |
+------------+
1 row in set (0.00 sec)

mysql> select date_add('2022-01-01', interval 3 month) as res;
+------------+
| res        |
+------------+
| 2022-04-01 |
+------------+
1 row in set (0.00 sec)

1.6日期减天数

sql 复制代码
mysql> select date_sub('2022-01-01', interval 12 day) as res;
+------------+
| res        |
+------------+
| 2021-12-20 |
+------------+
1 row in set (0.00 sec)

mysql> select date_sub('2022-01-01', interval 3 month) as res;
+------------+
| res        |
+------------+
| 2021-10-01 |
+------------+
1 row in set (0.00 sec)

1.7计算两个日期相隔多少天

sql 复制代码
mysql> select datediff('2024-03-24', '2022-01-01') as res;
+------+
| res  |
+------+
|  813 |
+------+
1 row in set (0.00 sec)


1.8案例

  • 创建一张生日表
sql 复制代码
mysql> create table bir(
    -> id int primary key auto_increment,
    -> birthday date
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into bir(birthday) values('1998-05-28');
Query OK, 1 row affected (0.00 sec)

mysql> insert into bir(birthday) values(current_date);
Query OK, 1 row affected (0.00 sec)

mysql> insert into bir(birthday) values(current_timestamp());
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into bir(birthday) values(date(current_timestamp()));
Query OK, 1 row affected (0.00 sec)

mysql> select * from bir;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2024-03-24 |
|  2 | 1998-05-28 |
|  3 | 2024-03-24 |
|  4 | 2024-03-24 |
+----+------------+
4 rows in set (0.00 sec)

第三次插入的为时间戳,mysql会自动转化为日期。

  • 创建一个留言表
sql 复制代码
mysql> create table mes(
    -> id int primary key auto_increment,
    -> message varchar(100) not null,
    -> sendtime datetime
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into mes (message, sendtime) values ('窗前明月光', now());
Query OK, 1 row affected (0.00 sec)

mysql> insert into mes (message, sendtime) values ('疑是地上霜', now());
Query OK, 1 row affected (0.00 sec)

mysql> select * from mes;
+----+-----------------+---------------------+
| id | message         | sendtime            |
+----+-----------------+---------------------+
|  1 | 窗前明月光      | 2024-03-24 22:29:33 |
|  2 | 疑是地上霜      | 2024-03-24 22:29:43 |
+----+-----------------+---------------------+
2 rows in set (0.00 sec)
  • 查询3分钟之内发的评论
sql 复制代码
mysql> select * from mes
+----+-----------------+---------------------+
| id | message         | sendtime            |
+----+-----------------+---------------------+
|  1 | 窗前明月光      | 2024-03-24 22:29:33 |
|  2 | 疑是地上霜      | 2024-03-24 22:29:43 |
|  3 | 举头望明月      | 2024-03-24 22:31:42 |
|  4 | 低头思故乡      | 2024-03-24 22:31:49 |
+----+-----------------+---------------------+
4 rows in set (0.00 sec)

mysql> select * from mes where sendtime > date_sub(now(), interval 3 minute);
+----+-----------------+---------------------+
| id | message         | sendtime            |
+----+-----------------+---------------------+
|  3 | 举头望明月      | 2024-03-24 22:31:42 |
|  4 | 低头思故乡      | 2024-03-24 22:31:49 |
+----+-----------------+---------------------+
2 rows in set (0.00 sec)

2.字符串函数

2.1获取字符集

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

mysql> select charset(123);
+--------------+
| charset(123) |
+--------------+
| binary       |
+--------------+
1 row in set (0.00 sec)

mysql> select charset(sendtime) from mes;
+-------------------+
| charset(sendtime) |
+-------------------+
| binary            |
| binary            |
| binary            |
| binary            |
+-------------------+
4 rows in set (0.00 sec)

2.2字符串拼接

类似于c语言中的strcat

sql 复制代码
mysql> select concat('123', 'hello', 'world') as res;
+---------------+
| res           |
+---------------+
| 123helloworld |
+---------------+
1 row in set (0.00 sec)
sql 复制代码
mysql> select concat('id: ', id, ' 发送了一条消息: ', message, ' 时间:', sendtime) as res from mes;
+-------------------------------------------------------------------------+
| res                                                                     |
+-------------------------------------------------------------------------+
| id: 1 发送了一条消息: 窗前明月光 时间:2024-03-24 22:29:33               |
| id: 2 发送了一条消息: 疑是地上霜 时间:2024-03-24 22:29:43               |
| id: 3 发送了一条消息: 举头望明月 时间:2024-03-24 22:31:42               |
| id: 4 发送了一条消息: 低头思故乡 时间:2024-03-24 22:31:49               |
+-------------------------------------------------------------------------+
4 rows in set (0.00 sec)

2.3字符串查询

instr(string, substring) 返回substring在string中的下标(从1开始),不存在返回0。

sql 复制代码
mysql> select instr('helfjsadio123fjkdsfa', '123');
+--------------------------------------+
| instr('helfjsadio123fjkdsfa', '123') |
+--------------------------------------+
|                                   11 |
+--------------------------------------+
1 row in set (0.00 sec)

2.4大小写转换

ucase转大写,lcase转小写。

sql 复制代码
mysql> select ucase('fAjfkjBSD213jD1s');
+---------------------------+
| ucase('fAjfkjBSD213jD1s') |
+---------------------------+
| FAJFKJBSD213JD1S          |
+---------------------------+
1 row in set (0.00 sec)
sql 复制代码
mysql> select lcase('fAjfkjBSD213jD1s');
+---------------------------+
| lcase('fAjfkjBSD213jD1s') |
+---------------------------+
| fajfkjbsd213jd1s          |
+---------------------------+
1 row in set (0.00 sec)

2.5字符串提取

left(string, length),string中从左往右提取length个字符。

sql 复制代码
mysql> select left('hello world', 8);
+------------------------+
| left('hello world', 8) |
+------------------------+
| hello wo               |
+------------------------+
1 row in set (0.00 sec)

right(string, length)),stringg中从右向左提取length个字符。

sql 复制代码
mysql> select right('hello world', 7);
+-------------------------+
| right('hello world', 7) |
+-------------------------+
| o world                 |
+-------------------------+
1 row in set (0.00 sec)

2.6字符串长度

sql 复制代码
mysql> select length('fds28f3j28x');
+-----------------------+
| length('fds28f3j28x') |
+-----------------------+
|                    11 |
+-----------------------+
1 row in set (0.00 sec)

案例:求学生表中学生姓名占用的字节数

sql 复制代码
mysql> select name, length(name) 长度 from exam_result;
+-----------+--------+
| name      | 长度   |
+-----------+--------+
| 唐三藏    |      9 |
| 猪悟能    |      9 |
| 曹孟德    |      9 |
| 刘玄德    |      9 |
| 孙权      |      6 |
| 宋公明    |      9 |
| 张翼德    |      9 |
| 关云长    |      9 |
+-----------+--------+
8 rows in set (0.00 sec)

length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关),这里是3个字节。

2.7字符串替换

replace(str, s1, s2),在str中,用s2替换s1。

sql 复制代码
mysql> select replace('hello world', 'world', 'mysql');
+------------------------------------------+
| replace('hello world', 'world', 'mysql') |
+------------------------------------------+
| hello mysql                              |
+------------------------------------------+
1 row in set (0.00 sec)

案例:将表中所有姓孙的同学改成姓周。

sql 复制代码
mysql> select name from exam_result;
+-----------+
| name      |
+-----------+
| 唐三藏    |
| 猪悟能    |
| 曹孟德    |
| 刘玄德    |
| 孙权      |
| 宋公明    |
| 张翼德    |
| 关云长    |
+-----------+
8 rows in set (0.00 sec)

mysql> select replace(name, '孙', '周') from exam_result;
+-----------------------------+
| replace(name, '孙', '周')   |
+-----------------------------+
| 唐三藏                      |
| 猪悟能                      |
| 曹孟德                      |
| 刘玄德                      |
| 周权                        |
| 宋公明                      |
| 张翼德                      |
| 关云长                      |
+-----------------------------+
8 rows in set (0.00 sec)

注意:select和replace配合使用并不会修改数据库中的数据。

2.8字符串截取

substring(str, pos, length),str中的pos位置开始截取length个字符。length不写表示截取完。

sql 复制代码
mysql> select substring('fja382nqf8q8n2nc', 4);
+----------------------------------+
| substring('fja382nqf8q8n2nc', 4) |
+----------------------------------+
| 382nqf8q8n2nc                    |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select substring('fja382nqf8q8n2nc', 4, 5);
+-------------------------------------+
| substring('fja382nqf8q8n2nc', 4, 5) |
+-------------------------------------+
| 382nq                               |
+-------------------------------------+
1 row in set (0.00 sec)

案例:截取name中第二个到第三个字段。

sql 复制代码
mysql> select name, substring(name, 2, 2) from exam_result;
+-----------+-----------------------+
| name      | substring(name, 2, 2) |
+-----------+-----------------------+
| 唐三藏    | 三藏                  |
| 猪悟能    | 悟能                  |
| 曹孟德    | 孟德                  |
| 刘玄德    | 玄德                  |
| 孙权      | 权                    |
| 宋公明    | 公明                  |
| 张翼德    | 翼德                  |
| 关云长    | 云长                  |
+-----------+-----------------------+
8 rows in set (0.00 sec)

2.9去除空格

ltrim去除左边空格,rtrim去除右边的空格。

sql 复制代码
mysql> select ltrim('  nihao  ') as res;
+---------+
| res     |
+---------+
| nihao   |
+---------+
1 row in set (0.00 sec)

mysql> select rtrim('  nihao  ') as res;
+---------+
| res     |
+---------+
|   nihao |
+---------+
1 row in set (0.00 sec)

3.数学函数

3.1绝对值

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

3.2转二进制

sql 复制代码
mysql> select bin(7);
+--------+
| bin(7) |
+--------+
| 111    |
+--------+
1 row in set (0.00 sec)

3.3转十六进制

sql 复制代码
mysql> select hex(41);
+---------+
| hex(41) |
+---------+
| 29      |
+---------+
1 row in set (0.00 sec)

3.4进制转换

任意两种进制之间的转换

sql 复制代码
mysql> select conv(21, 10, 4);
+-----------------+
| conv(21, 10, 4) |
+-----------------+
| 111             |
+-----------------+
1 row in set (0.00 sec)

十进制转四进制

3.5向上向下取整

ceiling向上取整,floor向下取整

sql 复制代码
mysql> select ceiling(2.11);
+---------------+
| ceiling(2.11) |
+---------------+
|             3 |
+---------------+
1 row in set (0.00 sec)

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

3.6保留n位小数

sql 复制代码
mysql> select format(1.234578, 5);
+---------------------+
| format(1.234578, 5) |
+---------------------+
| 1.23458             |
+---------------------+
1 row in set (0.00 sec)

3.7随机数

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

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

3.8取模

sql 复制代码
mysql> select mod(10, 3);
+------------+
| mod(10, 3) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

4.其他函数

4.1查询当前用户

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

4.2显示当前数据库

sql 复制代码
mysql> select database();
+------------+
| database() |
+------------+
| user5      |
+------------+
1 row in set (0.00 sec)

4.3md5摘要

如果当前数据库存储的是密码,并且是以明文的形式保存的,如果被黑客攻击了,拿到了数据库中所有人的密码,那就麻烦了,针对这种情况,保存密码可以使用md5形成一个32位的数据摘要。

sql 复制代码
mysql> create table user(
    -> id int primary key auto_increment,
    -> name varchar(20) not null,
    -> password char(32) not null
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into user(name, password) values('张三', 1234);
Query OK, 1 row affected (0.00 sec)

mysql> insert into user(name, password) values('李四', md5('1234'));
Query OK, 1 row affected (0.00 sec)

mysql> select * from user;
+----+--------+----------------------------------+
| id | name   | password                         |
+----+--------+----------------------------------+
|  1 | 张三   | 1234                             |
|  2 | 李四   | 81dc9bdb52d04dc20036dbd8313ed055 |
+----+--------+----------------------------------+
2 rows in set (0.00 sec)

当用户登录的时候,只需要使用用户名和md5后的摘要在表中查找,如果找到了说明密码正确

sql 复制代码
mysql> select * from user where name='李四' and password=md5('1234');
+----+--------+----------------------------------+
| id | name   | password                         |
+----+--------+----------------------------------+
|  2 | 李四   | 81dc9bdb52d04dc20036dbd8313ed055 |
+----+--------+----------------------------------+
1 row in set (0.00 sec)

4.4passwor加密

在上述例子当中,我们也可以使用passw进行加密

sql 复制代码
mysql> select password('1234');
+-------------------------------------------+
| password('1234')                          |
+-------------------------------------------+
| *A4B6157319038724E3560894F7F932C8886EBFCF |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select md5('1234');
+----------------------------------+
| md5('1234')                      |
+----------------------------------+
| 81dc9bdb52d04dc20036dbd8313ed055 |
+----------------------------------+
1 row in set (0.00 sec)

可以看到两种方式得到的摘要是不同的。

4.5ifnull

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

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

mysql> select ifnull(null, 456);
+-------------------+
| ifnull(null, 456) |
+-------------------+
|               456 |
+-------------------+
1 row in set (0.00 sec)
相关推荐
雪域迷影16 分钟前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
bug菌¹1 小时前
滚雪球学Oracle[4.2讲]:PL/SQL基础语法
数据库·oracle
逸巽散人1 小时前
SQL基础教程
数据库·sql·oracle
月空MoonSky2 小时前
Oracle中TRUNC()函数详解
数据库·sql·oracle
momo小菜pa2 小时前
【MySQL 06】表的增删查改
数据库·mysql
向上的车轮3 小时前
Django学习笔记二:数据库操作详解
数据库·django
编程老船长3 小时前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
全栈师3 小时前
SQL Server中关于个性化需求批量删除表的做法
数据库·oracle
Data 3174 小时前
Hive数仓操作(十七)
大数据·数据库·数据仓库·hive·hadoop
BergerLee4 小时前
对不经常变动的数据集合添加Redis缓存
数据库·redis·缓存