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

【MySQL】内置函数

​🌠 作者:@阿亮joy.
🎆专栏:《零基础入门MySQL》
🎇 座右铭:每个优秀的人都有一段沉默的时光,那段时光是付出了很多努力却得不到结果的日子,我们把它叫做扎根
在这里插入图片描述

目录

    • 👉函数👈
      • 日期函数
      • 字符串函数
      • 数学函数
      • 其它函数
    • 👉总结👈

👉函数👈

日期函数

在这里插入图片描述

  • 获得年月日:
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-05-16     |
+----------------+
1 row in set (0.00 sec)
  • 获得时分秒:
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 19:13:44       |
+----------------+
1 row in set (0.00 sec
  • 获得时间戳:
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-05-16 19:14:25 |
+---------------------+
1 row in set (0.01 sec)mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2023-05-16 19:38:20 |
+---------------------+
1 row in set (0.00 sec)

同时使用 current_date 和 current_time 就相当于 current_timestamp

mysql> select current_date() 日期, current_time() 时间;
+------------+----------+
| 日期       | 时间     |
+------------+----------+
| 2023-05-16 | 19:16:12 |
+------------+----------+
1 row in set (0.00 sec)
  • 在日期的基础上加日期:
-- 加上十天
mysql> select date_add('2023-5-16', interval 10 day);
+----------------------------------------+
| date_add('2023-5-16', interval 10 day) |
+----------------------------------------+
| 2023-05-26                             |
+----------------------------------------+
1 row in set (0.00 sec)-- 加上一年
mysql> select date_add('2023-5-16', interval 1 year);
+----------------------------------------+
| date_add('2023-5-16', interval 1 year) |
+----------------------------------------+
| 2024-05-16                             |
+----------------------------------------+
1 row in set (0.00 sec)-- 加上一秒
mysql> select date_add('2023-5-16', interval 1 second);
+------------------------------------------+
| date_add('2023-5-16', interval 1 second) |
+------------------------------------------+
| 2023-05-16 00:00:01                      |
+------------------------------------------+
1 row in set (0.00 sec)
  • 在日期的基础上减去时间:
-- 减去十天
mysql> select date_sub('2023-5-16', interval 10 day);
+----------------------------------------+
| date_sub('2023-5-16', interval 10 day) |
+----------------------------------------+
| 2023-05-06                             |
+----------------------------------------+
1 row in set (0.00 sec)-- 减去一年
mysql> select date_sub('2023-5-16', interval 1 year);
+----------------------------------------+
| date_sub('2023-5-16', interval 1 year) |
+----------------------------------------+
| 2022-05-16                             |
+----------------------------------------+
1 row in set (0.00 sec)-- 减去一秒
mysql> select date_sub('2023-5-16', interval 1 second);
+------------------------------------------+
| date_sub('2023-5-16', interval 1 second) |
+------------------------------------------+
| 2023-05-15 23:59:59                      |
+------------------------------------------+
1 row in set (0.00 sec)
  • 计算两个日期之间相差多少天:
mysql> select datediff('2023-5-26', '2023-05-06');
+-------------------------------------+
| datediff('2023-5-26', '2023-05-06') |
+-------------------------------------+
|                                  20 |
+-------------------------------------+
1 row in set (0.00 sec)mysql> select datediff(now(), '1949-10-01');
+-------------------------------+
| datediff(now(), '1949-10-01') |
+-------------------------------+
|                         26890 |
+-------------------------------+
1 row in set (0.00 sec)
  • 获取时间戳中的日期
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-05-16  |
+-------------+
1 row in set (0.00 sec)mysql> select date(current_timestamp());
+---------------------------+
| date(current_timestamp()) |
+---------------------------+
| 2023-05-16                |
+---------------------------+
1 row in set (0.00 sec)

创建生日表

mysql> create table birthday(-> id int primary key auto_increment,-> d date not null,-> t timestamp);mysql> desc birthday;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type      | Null | Key | Default           | Extra                       |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id    | int(11)   | NO   | PRI | NULL              | auto_increment              |
| d     | date      | NO   |     | NULL              |                             |
| t     | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)mysql> insert into birthday (d) values('1949-10-01');
Query OK, 1 row affected (0.00 sec)mysql> insert into birthday (d) values(current_date());
Query OK, 1 row affected (0.01 sec)-- 插入数据时,timestamp类型会默认更新为当前的最新时间
mysql> select * from birthday;
+----+------------+---------------------+
| id | d          | t                   |
+----+------------+---------------------+
|  1 | 1949-10-01 | 2023-05-16 19:30:43 |
|  2 | 2023-05-16 | 2023-05-16 19:30:54 |
+----+------------+---------------------+
2 rows in set (0.00 sec)-- 将t字段的类型更改为datetime类型
mysql> alter table birthday modify t datetime;
Query OK, 2 rows affected (0.05 sec)
Records: 2  Duplicates: 0  Warnings: 0mysql> desc birthday;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| d     | date     | NO   |     | NULL    |                |
| t     | datetime | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)mysql> select * from birthday;
+----+------------+---------------------+
| id | d          | t                   |
+----+------------+---------------------+
|  1 | 1949-10-01 | 2023-05-16 19:30:43 |
|  2 | 2023-05-16 | 2023-05-16 19:30:54 |
+----+------------+---------------------+
2 rows in set (0.00 sec)mysql> insert into birthday (d, t) values(current_date(), current_timestamp());
Query OK, 1 row affected (0.01 sec)mysql> select * from birthday;
+----+------------+---------------------+
| id | d          | t                   |
+----+------------+---------------------+
|  1 | 1949-10-01 | 2023-05-16 19:30:43 |
|  2 | 2023-05-16 | 2023-05-16 19:30:54 |
|  3 | 2023-05-16 | 2023-05-16 19:37:02 |
+----+------------+---------------------+
3 rows in set (0.00 sec)

创建留言表

mysql> create table msg(-> id int unsigned primary key auto_increment,-> content varchar(100) not null,-> sendtime datetime-> );
Query OK, 0 rows affected (0.06 sec)mysql> desc msg;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| content  | varchar(100)     | NO   |     | NULL    |                |
| sendtime | datetime         | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)-- 插入数据
mysql> insert into msg (content, sendtime) values('莫道桑榆晚,为霞尚漫天', now());
Query OK, 1 row affected (0.01 sec)mysql> insert into msg (content, sendtime) values('须知少年凌云志,曾许人间第一流', now());
Query OK, 1 row affected (0.00 sec)-- 显示所有留言及留言的时间
mysql> select * from msg;
+----+-----------------------------------------------+---------------------+
| id | content                                       | sendtime            |
+----+-----------------------------------------------+---------------------+
|  1 | 莫道桑榆晚,为霞尚漫天                        | 2023-05-16 19:58:10 |
|  2 | 须知少年凌云志,曾许人间第一流                | 2023-05-16 19:59:30 |
+----+-----------------------------------------------+---------------------+
2 rows in set (0.00 sec)-- 查询在两分钟内发布的帖子
mysql> insert into msg (content, sendtime) values('仰天长笑出门去,我辈岂是蓬蒿人', now());
Query OK, 1 row affected (0.01 sec)mysql> select * from msg where date_add(sendtime, interval 2 minute) > now();
+----+-----------------------------------------------+---------------------+
| id | content                                       | sendtime            |
+----+-----------------------------------------------+---------------------+
|  3 | 仰天长笑出门去,我辈岂是蓬蒿人                | 2023-05-16 20:03:37 |
+----+-----------------------------------------------+---------------------+
1 row in set (0.00 sec)-- 理解:
------------------------------|-----------|-------------|------------------初始时间     now()       初始时间+2min

字符串函数

在这里插入图片描述

  • 查看字符串的字符集
-- 此处字符串的字符集与配置文件的默认字符集有关
mysql> select charset('hello world!');
+-------------------------+
| charset('hello world!') |
+-------------------------+
| utf8                    |
+-------------------------+
1 row in set (0.00 sec)mysql> show variables like 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
  • 获取 emp 表的 ename 和 deptno 列的字符集
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.00 sec)mysql> select charset(deptno) from emp;
+-----------------+
| charset(deptno) |
+-----------------+
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
| binary          |
+-----------------+
14 rows in set (0.00 sec)
  • 数字能够直接使用二进制来表示,不需要再进行编码,所以数字的 charset 为 binary,而字符串可能需要进行编码,其 charset 与配置文件中的 charset 和建表时指定的 charset 有关。

  • 当出现乱码问题时,可能是因为存储数据的编码和取出数据的编码是不一样的。出现乱码问题时,可能查看 charset 是否一致。

  • 字符串拼接

mysql> select concat('hello ', 'world!');
+----------------------------+
| concat('hello ', 'world!') |
+----------------------------+
| hello world!               |
+----------------------------+
1 row in set (0.00 sec)-- 尽管是数字也能够直接拼接
mysql> select concat('hello ', 123, ' ', '3.1415926');
+-----------------------------------------+
| concat('hello ', 123, ' ', '3.1415926') |
+-----------------------------------------+
| hello 123 3.1415926                     |
+-----------------------------------------+
1 row in set (0.00 sec)
  • 显示 exam_result 表中的信息,显示格式:“XXX的语文XXX分,数学XXX分,英语XXX分
mysql> select concat(name, '的语文', chinese, '分,数学', math, '分,英语', english, '分') 全班成绩 from exam_result;
+-------------------------------------------------+
| 全班成绩                                        |
+-------------------------------------------------+
| 唐三藏的语文67,数学98,英语56|
| 孙悟空的语文87,数学78,英语77|
| 猪悟能的语文88,数学98,英语90|
| 曹孟德的语文82,数学84,英语67|
| 刘玄德的语文55,数学85,英语45|
| 孙权的语文70,数学73,英语78|
| 宋公明的语文75,数学65,英语30|
+-------------------------------------------------+
7 rows in set (0.00 sec)
  • 求字符串的长度
mysql> select length('hello world!');
+------------------------+
| length('hello world!') |
+------------------------+
|                     12 |
+------------------------+
1 row in set (0.00 sec)mysql> select length(3.1415926);
+-------------------+
| length(3.1415926) |
+-------------------+
|                 9 |
+-------------------+
1 row in set (0.00 sec)-- 求出月薪超过两千的人的名字的长度
mysql> select ename, sal, length(ename) len from emp where sal>2000;
+-------+---------+------+
| ename | sal     | len  |
+-------+---------+------+
| JONES | 2975.00 |    5 |
| BLAKE | 2850.00 |    5 |
| CLARK | 2450.00 |    5 |
| SCOTT | 3000.00 |    5 |
| KING  | 5000.00 |    4 |
| FORD  | 3000.00 |    4 |
+-------+---------+------+
6 rows in set (0.00 sec)

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

-- utf-8编码的一个汉字占三个字节
mysql> select length('你好');
+------------------+
| length('你好')   |
+------------------+
|                6 |
+------------------+
1 row in set (0.00 sec)
  • 获取字符串的子串
mysql> select substring('123456', 3);
+------------------------+
| substring('123456', 3) |
+------------------------+
| 3456                   |
+------------------------+
1 row in set (0.00 sec)mysql> select substring('123456', 0);
+------------------------+
| substring('123456', 0) |
+------------------------+
|                        |
+------------------------+
1 row in set (0.00 sec)mysql> select substring('123456', 3, 4);
+---------------------------+
| substring('123456', 3, 4) |
+---------------------------+
| 3456                      |
+---------------------------+
1 row in set (0.00 sec)mysql> select substring('你好,世界', 1, 2);
+------------------------------------+
| substring('你好,世界', 1, 2)      |
+------------------------------------+
| 你好                               |
+------------------------------------+
1 row in set (0.00 sec)mysql> select substring('你好,世界', 1, 4);
+------------------------------------+
| substring('你好,世界', 1, 4)      |
+------------------------------------+
| 你好,世                           |
+------------------------------------+
1 row in set (0.00 sec)
  • 数据库的字符串下标是从 1 开始的,原因是使用数据库的人可能不是程序员,只是普通用户。
  • substring 截取子串就是按照一个个字符来截取的,而不是按照字节来截取的。
  • substring 的第一个参数是要截取的字符串,第二个参数是截取的起始位置,第三个参数是截取多少个字符。如果省略第三个参数,默认截取到字符串的最后一个字符。
  • 字符串替换
-- 'xyz'是要被替换的字符串,'XYZ'是用来替换的字符串
-- 如果原字符串中没有要被替换的字符串,则无法完成替换
mysql> select replace('abcdxyz1234', 'xyz', 'XYZ');
+--------------------------------------+
| replace('abcdxyz1234', 'xyz', 'XYZ') |
+--------------------------------------+
| abcdXYZ1234                          |
+--------------------------------------+
1 row in set (0.00 sec)-- replace是全部替换,而不是部分替换
mysql> select replace('abcdxyz1234xyz', 'xyz', 'XYZ');
+-----------------------------------------+
| replace('abcdxyz1234xyz', 'xyz', 'XYZ') |
+-----------------------------------------+
| abcdXYZ1234XYZ                          |
+-----------------------------------------+
1 row in set (0.00 sec)
  • 大小写转换
mysql> select lcase('ABCD');
+---------------+
| lcase('ABCD') |
+---------------+
| abcd          |
+---------------+
1 row in set (0.00 sec)mysql> select ucase('abcd');
+---------------+
| ucase('abcd') |
+---------------+
| ABCD          |
+---------------+
1 row in set (0.00 sec)

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

mysql> select concat(ucase(substring(ename, 1, 1)), lcase(substring(ename, 2))) name from emp;
+--------+
| name   |
+--------+
| Smith  |
| Allen  |
| Ward   |
| Jones  |
| Martin |
| Blake  |
| Clark  |
| Scott  |
| King   |
| Turner |
| Adams  |
| James  |
| Ford   |
| Miller |
+--------+
14 rows in set (0.00 sec)
  • 查找子串
-- instr返回的是子串第一次出现的位置,没有出现则返回零
-- instr查找子串也是以字符为单位的,而不是以字节为单位
mysql> select instr('abcd1234', 'b');
+------------------------+
| instr('abcd1234', 'b') |
+------------------------+
|                      2 |
+------------------------+
1 row in set (0.00 sec)mysql> select instr('abcd1234', 4);
+----------------------+
| instr('abcd1234', 4) |
+----------------------+
|                    8 |
+----------------------+
1 row in set (0.00 sec)mysql> select instr('abcd1234', 'e');
+------------------------+
| instr('abcd1234', 'e') |
+------------------------+
|                      0 |
+------------------------+
1 row in set (0.00 sec)

找出名字中包含 TH 的员工

-- 使用通配符
mysql> select * from emp where ename like '%TH%';
+--------+-------+-------+------+---------------------+--------+------+--------+
| empno  | ename | job   | mgr  | hiredate            | sal    | comm | deptno |
+--------+-------+-------+------+---------------------+--------+------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL |     20 |
+--------+-------+-------+------+---------------------+--------+------+--------+
1 row in set (0.00 sec)-- 内置函数也可以出现在where子句中
mysql> select * from emp where instr(ename, 'TH');
+--------+-------+-------+------+---------------------+--------+------+--------+
| empno  | ename | job   | mgr  | hiredate            | sal    | comm | deptno |
+--------+-------+-------+------+---------------------+--------+------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL |     20 |
+--------+-------+-------+------+---------------------+--------+------+--------+
1 row in set (0.00 sec)
  • 字符串比较
-- 和C语言的strcmp函数一直,一直比较两个字符串中
-- 的字符直到比较出结果或者直到对方到字符串的末尾
-- 字符的大小是按照ASCII码进行比较的
mysql> select strcmp('abc', 'abcd');
+-----------------------+
| strcmp('abc', 'abcd') |
+-----------------------+
|                    -1 |
+-----------------------+
1 row in set (0.00 sec)mysql> select strcmp('abcd', 'abcd');
+------------------------+
| strcmp('abcd', 'abcd') |
+------------------------+
|                      0 |
+------------------------+
1 row in set (0.00 sec)mysql> select strcmp('abce', 'abcd');
+------------------------+
| strcmp('abce', 'abcd') |
+------------------------+
|                      1 |
+------------------------+
1 row in set (0.00 sec)-- 找出岗位是CLERK的所有员工
mysql> select * from emp where strcmp(job, 'CLERK') = 0;
+--------+--------+-------+------+---------------------+---------+------+--------+
| empno  | ename  | job   | mgr  | hiredate            | sal     | comm | deptno |
+--------+--------+-------+------+---------------------+---------+------+--------+
| 007369 | SMITH  | CLERK | 7902 | 1980-12-17 00:00:00 |  800.00 | NULL |     20 |
| 007876 | ADAMS  | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL |     20 |
| 007900 | JAMES  | CLERK | 7698 | 1981-12-03 00:00:00 |  950.00 | NULL |     30 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL |     10 |
+--------+--------+-------+------+---------------------+---------+------+--------+
4 rows in set (0.00 sec)
  • 从左右截取字符串
mysql> select left('abcd', 1);
+-----------------+
| left('abcd', 1) |
+-----------------+
| a               |
+-----------------+
1 row in set (0.00 sec)mysql> select left('abcd', 3);
+-----------------+
| left('abcd', 3) |
+-----------------+
| abc             |
+-----------------+
1 row in set (0.00 sec)mysql> select right('abcd', 3);
+------------------+
| right('abcd', 3) |
+------------------+
| bcd              |
+------------------+
1 row in set (0.00 sec)mysql> select right('abcd', 1);
+------------------+
| right('abcd', 1) |
+------------------+
| d                |
+------------------+
1 row in set (0.00 sec)
  • 去重空格
-- trim不会去除字符串中间的空格
mysql> select trim('abcd 1234') ret;
+-----------+
| ret       |
+-----------+
| abcd 1234 |
+-----------+
1 row in set (0.00 sec)-- trim会去除左右两边的空格
mysql> select trim('   abcd 1234  ') ret;
+-----------+
| ret       |
+-----------+
| abcd 1234 |
+-----------+
1 row in set (0.00 sec)-- ltrim只会去除字符串左边的空格
mysql> select ltrim('   abcd 1234  ') ret;
+-------------+
| ret         |
+-------------+
| abcd 1234   |
+-------------+
1 row in set (0.00 sec)-- rtrim只会去除字符串右边的空格
mysql> select rtrim('   abcd 1234  ') ret;
+--------------+
| ret          |
+--------------+
|    abcd 1234 |
+--------------+
1 row in set (0.00 sec)

数学函数

在这里插入图片描述

  • 绝对值
mysql> select abs(-10);
+----------+
| abs(-10) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)mysql> select abs(-3.14);
+------------+
| abs(-3.14) |
+------------+
|       3.14 |
+------------+
1 row in set (0.00 sec)
  • 十进制转二进制
mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010    |
+---------+
1 row in set (0.00 sec)mysql> select bin(-1);
+------------------------------------------------------------------+
| bin(-1)                                                          |
+------------------------------------------------------------------+
| 1111111111111111111111111111111111111111111111111111111111111111 |
+------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 十进制转十六进制
mysql> select hex(10);
+---------+
| hex(10) |
+---------+
| A       |
+---------+
1 row in set (0.00 sec)mysql> select hex(32);
+---------+
| hex(32) |
+---------+
| 20      |
+---------+
1 row in set (0.00 sec)
  • 进制转换
-- 将十进制的10转成二进制
mysql> select conv(10, 10, 2);
+-----------------+
| conv(10, 10, 2) |
+-----------------+
| 1010            |
+-----------------+
1 row in set (0.00 sec)-- 将二进制的10转成十进制
mysql> select conv(10, 2, 10);
+-----------------+
| conv(10, 2, 10) |
+-----------------+
| 2               |
+-----------------+
1 row in set (0.00 sec)
  • 向上取整
mysql> select ceiling(3.14);
+---------------+
| ceiling(3.14) |
+---------------+
|             4 |
+---------------+
1 row in set (0.00 sec)mysql> select ceiling(-3.14);
+----------------+
| ceiling(-3.14) |
+----------------+
|             -3 |
+----------------+
1 row in set (0.00 sec)
  • 向下取整
mysql> select floor(3.14);
+-------------+
| floor(3.14) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)mysql> select floor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
|           -4 |
+--------------+
1 row in set (0.00 sec)
  • 格式化
-- 保留2位小数位数(小数四舍五入)
mysql> select format(12.3456, 2);
+--------------------+
| format(12.3456, 2) |
+--------------------+
| 12.35              |
+--------------------+
1 row in set (0.00 sec)mysql> select format(100, 2);
+----------------+
| format(100, 2) |
+----------------+
| 100.00         |
+----------------+
1 row in set (0.00 sec)
  • 随机数
-- rand函数随机产生[0.0, 1.0)之间的数
mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.6638928788377837 |
+--------------------+
1 row in set (0.00 sec)mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.21991669872767916 |
+---------------------+
1 row in set (0.00 sec)-- 对rand函数进行加减乘除就可以拼出任意区间的随机数
mysql> select rand()-rand();
+---------------------+
| rand()-rand()       |
+---------------------+
| -0.7718694859853661 |
+---------------------+
1 row in set (0.00 sec)-- 产生[0, 100)之间的随机数
mysql> select 100*rand();
+------------------+
| 100*rand()       |
+------------------+
| 45.6843777053844 |
+------------------+
1 row in set (0.00 sec)

rand 函数可以配合 format 函数随机出整数。

  • 取模
mysql> select mod(1, 3);
+-----------+
| mod(1, 3) |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)mysql> select mod(-1, 3);
+------------+
| mod(-1, 3) |
+------------+
|         -1 |
+------------+
1 row in set (0.00 sec)mysql> select mod(3.14, 2);
+--------------+
| mod(3.14, 2) |
+--------------+
|         1.14 |
+--------------+
1 row in set (0.00 sec)

注:有关负数取模的原则,可以自行查询,本人就不赘述了。

其它函数

  • 查询当前用户
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

在这里插入图片描述

在这里插入图片描述

查看 user 表

mysql> use mysql;
mysql> select * from user;
-- 查看user表结构
mysql> desc user;

在这里插入图片描述
在这里插入图片描述
user 函数的实现原理

-- user函数可以通过select加concat函数来实现
mysql> select concat(User, '@', Host) `user()` from user where User='root';
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
  • md5 函数
mysql> select md5('a');
+----------------------------------+
| md5('a')                         |
+----------------------------------+
| 0cc175b9c0f1b6a831c399e269772661 |
+----------------------------------+
1 row in set (0.00 sec)mysql> select md5('ab');
+----------------------------------+
| md5('ab')                        |
+----------------------------------+
| 187ef4436122d1cc2f40dc2b92f0eba0 |
+----------------------------------+
1 row in set (0.00 sec)
  • md5 算法对一个字符串进行 md5 摘要,摘要后等到一个 32 位的字符串,不管原字符串是多长。
  • 尽管两个字符串只相差一个字符,md5 算法形成的两个摘要相差也很大。
  • md5 算法可以对秘密进行摘要,从而起到用户的密码,因为很难通过 md5 算法形成的摘要破解出原密码。
-- authentication_string 是密码通过md5算法形成的摘要
mysql> select user, host, authentication_string from user;
mysql> select user, host, authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *4A4A4E1077BBA88E4537392A2D56040FF27F1FC1 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

模拟用户注册的场景

-- 在自己的数据库中进行模拟
mysql> select database();
+------------+
| database() |
+------------+
| my_db      |
+------------+
1 row in set (0.00 sec)-- 创建用户表
mysql> create table if not exists user(-> id bigint unsigned primary key auto_increment,-> name varchar(32) not null,-> password char(64) not null,-> reg_time datetime not null-> );
Query OK, 0 rows affected (0.02 sec)mysql> insert into user (name, password, reg_time) values('张三', md5('123456'), now());
Query OK, 1 row affected (0.00 sec)mysql> select * from user;
+----+--------+----------------------------------+---------------------+
| id | name   | password                         | reg_time            |
+----+--------+----------------------------------+---------------------+
|  1 | 张三   | e10adc3949ba59abbe56e057f20f883e | 2023-05-16 23:17:41 |
+----+--------+----------------------------------+---------------------+
1 row in set (0.00 sec)mysql> select * from user where md5('123456') = password;
+----+--------+----------------------------------+---------------------+
| id | name   | password                         | reg_time            |
+----+--------+----------------------------------+---------------------+
|  1 | 张三   | e10adc3949ba59abbe56e057f20f883e | 2023-05-16 23:17:41 |
+----+--------+----------------------------------+---------------------+
1 row in set (0.00 sec)
  • 用户表中储存的密码都不是真正用户的密码,都是摘要来的,因此后端进行用户认证比较的都是摘要。
  • 涉及到 md5 算法和密码等私密的信息,无法通过上翻来找到历史输过的 SQL 语句。

  • password 函数
-- MySQL数据库使用password函数对用户加密
mysql> select password('123456');
+-------------------------------------------+
| password('123456')                        |
+-------------------------------------------+
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)mysql> select length(password('123456'));
+----------------------------+
| length(password('123456')) |
+----------------------------+
|                         41 |
+----------------------------+
1 row in set, 1 warning (0.00 sec)
  • ifnull 函数
-- ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
-- ifnull就相当于是三目运算符mysql> select ifnull('a', 'b');
+------------------+
| ifnull('a', 'b') |
+------------------+
| a                |
+------------------+
1 row in set (0.00 sec)mysql> select ifnull(null, 'b');
+-------------------+
| ifnull(null, 'b') |
+-------------------+
| b                 |
+-------------------+
1 row in set (0.00 sec)mysql> select ifnull('a', null);
+-------------------+
| ifnull('a', null) |
+-------------------+
| a                 |
+-------------------+
1 row in set (0.00 sec)

👉总结👈

本篇博客主要讲解了日期函数、字符串函数、数学函数、md5 函数、password 函数以及 ifnull 函数等等。以上就是本篇博客的全部内容了,如果大家觉得有收获的话,可以点个三连支持一下!谢谢大家啦!💖💝❣️

相关文章:

【MySQL】内置函数

​🌠 作者:阿亮joy. 🎆专栏:《零基础入门MySQL》 🎇 座右铭:每个优秀的人都有一段沉默的时光,那段时光是付出了很多努力却得不到结果的日子,我们把它叫做扎根 目录 👉函…...

使用arm-none-eabi-gcc编译器搭建STM32的Vscode开发环境

工具 make:Windows中没有make,但是可以通过安装MinGW或者MinGW-w64,得到make。gcc-arm-none-eabi:建议最新版,防止调试报错OpenOCDvscodecubeMX VSCODE 插件 Arm Assembly:汇编文件解析C/C:c…...

图数据库Neo4j学习二——cypher基本语法

1命名规范 名称应以字母字符开头,不以数字开头,名称不应包含符号,下划线除外可以很长,最多65535( 2^16 - 1) 或65534字符,具体取决于 Neo4j 的版本名称区分大小写。:PERSON和:Person是:person三个不同的标签&#xff…...

ChatGPT:人工智能交互的未来之光

一、ChatGPT:开启自然语言交流新纪元 ChatGPT 是基于 GPT(生成式预训练)技术的最新版本,它采用深度学习模型,通过在大规模文本数据上的预训练来理解自然语言,并生成具有连贯性和合理性的回复。ChatGPT 是一…...

128最长连续数组

题目描述 最长连续序列 https://leetcode.cn/problems/longest-consecutive-sequence/class Solution {public:int longestConsecutive(vector<int>& nums) {unordered_set<int> st(...

redis 1

shell 1&#xff1a;安装1. 源码安装&#xff08;CENTOS&#xff09; 2.999:可能会出现得问题1. 编译出错 1&#xff1a;安装 1. 源码安装&#xff08;CENTOS&#xff09; 官方下载源码包 wget https://download.redis.io/redis-stable.tar.gz # 安装依赖 yum install gcc解压…...

vue+Element项目中v-for循环+表单验证

如果在Form 表单里有通过v-for动态生成&#xff0c;如何设置验证呢&#xff1f; <el-form ref"ruleFormRef" :model"ruleForm" status-icon :rules"rules" label-width"120px"class"demo-ruleForm" hide-required-aster…...

Day 66-68 主动学习之ALEC

代码&#xff1a; package dl;import java.io.FileReader; import java.util.*; import weka.core.Instances;/*** Active learning through density clustering.*/ public class Alec {/*** The whole dataset.*/Instances dataset;/*** The maximal number of queries that …...

local-path-provisioner与pvc本地磁盘挂载helm部署

1.helm拉取安装包 helm repo add containeroo https://charts.containeroo.ch helm pull containeroo/local-path-provisioner --version 0.0.19 tar -zxvf local-path-provisioner-0.0.19.tgz cd local-path-provisioner mv values.yaml values.yaml.back grep -v "#&qu…...

Visio/PPT/Matlab输出300dpi以上图片【满足标准投稿要求】

1. visio 遵照如下输出选项&#xff0c;另存为tif格式文件时&#xff0c;选择正确输出便是300dpi以上 2. matlab 文件选项选中导出设置&#xff0c;在渲染中选择dpi为600&#xff0c;导出图片即可&#xff0c;科研建议选择tif格式文件 3.ppt 打开注册表&#xff0c;winr键…...

科技UI图标的制作

科技UI图标的制作&#xff0c;效果图如下&#xff1a; 一、新建合成 1、新建合成&#xff0c;命名为合成1&#xff0c;参数设置如下&#xff1a; 2、新建纯色&#xff0c;命名为分形 二、添加分形杂色 1、添加分形杂色 为纯色层“分形”&#xff0c;添加分形杂色&#xff0c…...

微信小程序将接口返回的文件流预览导出Excel文件并转发

把接口url替换就可以用了 exportExcel () {wx.request({url: importMyApply, //这个地方是你获取二进制流的接口地址method: POST,responseType: "arraybuffer", //特别注意的是此处是请求文件流必须加上的属性&#xff0c;不然你导出到手机上的时候打不开&#xff…...

windows 安装 mongodb 数据库

软件下载 访问官方的下载地址&#xff1a; https://www.mongodb.com/try/download/community &#xff0c;然后选择对应的版本进行下载 下载好了之后双击进行安装 软件安装 1、点击 next 点击下一步 2、勾选接受协议&#xff0c;点击 next 3、第三页有两个选项&#x…...

业务不打烊:解决软件系统升级痛点的新方法

数字化时代&#xff0c;随着用户对产品性能和功能要求的不断提升&#xff0c;应用服务升级成了企业保持竞争力的关键之一。然而&#xff0c;传统的应用服务升级往往会给用户带来不必要的中断和不便&#xff0c;这种“伤筋动骨”的升级方式已经无法满足日益增长的用户需求&#…...

csdn新星计划vue3+ts+antd赛道——利用inscode搭建vue3(ts)+antd前端模板

文章目录 ⭐前言⭐利用inscode免费开放资源&#x1f496; 在inscode搭建vue3tsant项目&#x1f496; 调整配置&#x1f496; antd 国际化配置&#x1f496; 用户store&#x1f496; 路由权限&#x1f496; 预览 ⭐结束 ⭐前言 大家好&#xff0c;我是yma16&#xff0c;本文分享…...

通过 CSS 的样式实现语音发送动效类似声音震动的效果

实现效果&#xff1a;一般用于发送语音的时候&#xff0c;出现动画效果 //模版部分 <view class"musical-scale"><view class"scale"><view class"em" v-for"(item,index) in 15" :key"index"></view…...

【C#】.Net Framework框架使用JWT

2023年&#xff0c;第31周&#xff0c;第2篇文章。给自己一个目标&#xff0c;然后坚持总会有收货&#xff0c;不信你试试&#xff01; 本篇文章主要简单讲讲&#xff0c;.Net Framework框架下使用JWT的代码例子&#xff0c;以及他们的基本概念。 2002年微软发布了.net framewo…...

SQL高级教程第三章

SQL CREATE DATABASE 语句 CREATE DATABASE 语句 CREATE DATABASE 用于创建数据库。 SQL CREATE DATABASE 语法 CREATE DATABASE database_name SQL CREATE DATABASE 实例 现在我们希望创建一个名为 "my_db" 的数据库。 我们使用下面的 CREATE DATABASE 语句&…...

vue 3.0 下载本地pdf文件

使用a标签,把pdf文件放到public文件夹下面 <el-form label-width"160px"> <el-form-item label"使用手册"> <div class"form-item-static"> <a href"/使用手册.pdf" target"_blank" class"link&q…...

平板用的触控笔什么牌子好?ipad第三方电容笔推荐

随着技术的发展&#xff0c;出现了各种各样的平板电容笔。一支好的电容笔&#xff0c;不但可以极大地提升我们的工作效率&#xff0c;还可以极大地提升我们的学习效果。平替的电容笔&#xff0c;无论是在技术方面&#xff0c;还是在质量方面&#xff0c;都还有很大的提升空间&a…...

【Unity2D】相机移动以及设置相机边界

添加相机 添加相机时&#xff0c;首先需要在unity中添加 Cinemachine 包 第一次使用这个包时&#xff0c;需要在Package Manager中搜索并安装 安装Camera Mechine包后&#xff0c;添加2D Camera 设置跟随对象为Ruby &#xff08;从Hierarchy中将Ruby拖动到Follow中&#xff0…...

和chatgpt学架构04-路由开发

目录 1 什么是路由2 如何设置路由2.1 安装依赖2.2 创建路由文件2.3 创建首页2.4 编写HomePage2.5 更新路由配置2.6 让路由生效 3 测试总结 要想使用vue实现页面的灵活跳转&#xff0c;其中路由配置是必不可少的&#xff0c;我们在做开发的时候&#xff0c;先需要了解知识点&…...

Spring MVC异常处理【单个控制异常处理器、全局异常处理器、自定义异常处理器】

目录 一、单个控制器异常处理 1.1 控制器方法 1.2 编写出错页面 1.3 测试结果 二、全局异常处理 2.1 一个有异常的控制器类 2.2 全局异常处理器类 2.3 测试结果 三、自定义异常处理器 3.1 自定义异常处理器 3.2 测试结果 往期专栏&文章相关导读 1. Maven系列…...

使用3ds Max粒子系统创建飞天箭雨特效场景

推荐&#xff1a; NSDT场景编辑器助你快速搭建可二次开发的3D应用场景 1. 设置箭头 步骤 1 打开 3ds Max。 打开 3ds Max 步骤 2 我使用多边形建模技术制作了一个简单的箭头&#xff0c;我将 在教程中使用。.max您可以从 下载部分。 箭头.max 步骤 3 将此箭头重命名为静态…...

【朴素贝叶斯实例】

朴素贝叶斯对新闻进行分类 朴素贝叶斯算法是一种常用的文本分类方法&#xff0c;特别适用于自然语言处理任务&#xff0c;如新闻分类。在这篇博客中&#xff0c;我们将使用Python的scikit-learn库来实现朴素贝叶斯算法&#xff0c;并将其应用于新闻分类任务。 数据准备 首先…...

MPAS跨尺度、可变分辨率模式

跨尺度预测模式&#xff08;The Model for Prediction Across Scales - MPAS&#xff09;是由洛斯阿拉莫斯实验室和美国国家大气研究中心(NCAR)共同开发&#xff0c;其由3个部分组成&#xff0c;分别称为 MPAS-A&#xff08;大气模型&#xff09;、MPAS-O&#xff08;海洋模型&…...

微信小程序对接SSE接口记录

微信小程序对接SSE接口记录 需求&#xff1a;公司项目对接gpt&#xff0c;gpt产生的结果是分段返回&#xff0c;所以要求在产生结果时&#xff0c;有打字机的效果。原本是由定时器调用&#xff0c;后来优化改为服务端使用SSE接口。小程序使用起来比较方便&#xff0c;但是要求…...

Ngrok 的绝佳替代品,内网穿透神器 Serveo

什么是 Serveo Serveo 是一个免费的内网穿透服务&#xff0c;Serveo 可以将本地计算机暴露在互联网上&#xff0c;官方声称其为 Ngrok 的绝佳替代品。 Serveo 其最大优点是使用现有的 SSH 客户端&#xff0c;无需安装任何客户端软件即可完成端口转发。 Serveo 工作原理很简单…...

网络知识点之-路由

路由&#xff08;routing&#xff09;是指分组从源到目的地时&#xff0c;决定端到端路径的网络范围的进程。路由工作在OSI参考模型第三层——网络层的数据包转发设备。路由器通过转发数据包来实现网络互连。虽然路由器可以支持多种协议&#xff08;如TCP/IP、IPX/SPX、AppleTa…...

input 框如何移动光标,设置光标位置?

获取 input 光标位置 const inputDom document.getElementById("input") const selectionStart inputDom.selectionStart设置 input 光标 inputDom.focus() // focus() 异步&#xff0c;所以加了 setTimeout setTimeout(() > {const nextSelection selection…...