当前位置: 首页 > news >正文

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获得年月日

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-03-24     |
+----------------+
1 row in set (0.00 sec)

1.2获得时分秒

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

1.3获得年月日时分秒

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

1.4获得时间戳

会以日期的方式显示。

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

1.5日期加天数

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日期减天数

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计算两个日期相隔多少天

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


1.8案例

  • 创建一张生日表
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会自动转化为日期。

  • 创建一个留言表
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分钟之内发的评论
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获取字符集

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

mysql> select concat('123', 'hello', 'world') as res;
+---------------+
| res           |
+---------------+
| 123helloworld |
+---------------+
1 row in set (0.00 sec)
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。

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

2.4大小写转换

ucase转大写,lcase转小写。

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

2.5字符串提取

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

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

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

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

2.6字符串长度

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

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

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。

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

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

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不写表示截取完。

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中第二个到第三个字段。

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去除右边的空格。

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绝对值

mysql> select abs(-21);
+----------+
| abs(-21) |
+----------+
|       21 |
+----------+
1 row in set (0.00 sec)

3.2转二进制

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

3.3转十六进制

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

3.4进制转换

任意两种进制之间的转换

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

十进制转四进制

3.5向上向下取整

ceiling向上取整,floor向下取整

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位小数

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

3.7随机数

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取模

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

4.其他函数

4.1查询当前用户

mysql> select user();
+--------+
| user() |
+--------+
| root@  |
+--------+
1 row in set (0.00 sec)

4.2显示当前数据库

mysql> select database();
+------------+
| database() |
+------------+
| user5      |
+------------+
1 row in set (0.00 sec)

4.3md5摘要

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

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后的摘要在表中查找,如果找到了说明密码正确

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进行加密

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的值

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)

相关文章:

  • 机器学习 - save和load训练好的模型
  • PHP 读取嵌入式数据 SQLite3
  • 一个单生产-多消费模式下无锁方案(ygluu/卢益贵)
  • 数字乡村引领新风尚:科技赋能农村实现全面进步
  • 零基础机器学习(3)之机器学习的一般过程
  • GPT4.0
  • [flask]flask的路由
  • 35 跨域相关问题, 以及常见的解决方式
  • 【笔记】Hbase基础笔记
  • 刚进公司第一天-电脑环境搭建
  • 基于AT89C51单片机的智能交通灯设计
  • APS54083 大功率深度调光降压恒流驱动IC PWM 线性调光 车灯IC
  • 数据加载器
  • AI开源概览及工具使用
  • 在Sequence中缓存Niagara粒子轨道
  • CI/CD实战-jenkins部署 3
  • css的text-shadow详解
  • Python并发编程的三种方式:多线程(threading)、多进程(multiprocessing),以及基于协程的异步I/O(asyncio)
  • 02 React 组件使用
  • RAFT: Adapting Language Model to Domain Specific RAG
  • https://app.hackthebox.com/machines/Inject
  • Spring —— Spring简单的读取和存储对象 Ⅱ
  • 渗透测试之冰蝎实战
  • Mybatis、TKMybatis对比
  • Microsoft Office 2019(2022年10月批量许可版)图文教程
  • 《谷粒商城基础篇》分布式基础环境搭建
  • 哈希表题目:砖墙
  • Vue 3.0 选项 生命周期钩子
  • 【车载嵌入式开发】AutoSar架构入门介绍篇
  • 【计算机视觉 | 目标检测】DETR风格的目标检测框架解读