【MySQL内置函数】

2023-12-17 00:46:41

前言

剑指offer:一年又7天


一、日期函数

函数名称描述
current_date()当前日期:年月日
current_time()当前时间:时分秒
current_timestamp()当前时间戳:年月日 时分秒
date_add(date, interval num d_value_type)在date中添加日期或时间
d_value_type可选类型:year、month、day、hour、minute、second
date_sub(date, interval num d_value_type)在date中减去日期或时间
datediff(date1, date2)两个日期的差:date1 - date2,单位是天
now()当前日期时间( 同current_timestamp() )

案例:

获取日期

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-11-25     |
+----------------+
1 row in set (0.00 sec)

获取时间

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 15:26:57       |
+----------------+
1 row in set (0.00 sec)

获取时间戳

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-11-25 15:27:17 |
+---------------------+
1 row in set (0.00 sec)

在日期上增加时间

-- 当前日期
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-11-25     |
+----------------+
1 row in set (0.01 sec)
-- 当前日期加10天
mysql> select date_add(current_date(), interval 10 day);
+-------------------------------------------+
| date_add(current_date(), interval 10 day) |
+-------------------------------------------+
| 2023-12-05                                |
+-------------------------------------------+
1 row in set (0.00 sec)
-- 指定日期加10天
mysql> select date_add('2000-1-1', interval 10 day);
+---------------------------------------+
| date_add('2000-1-1', interval 10 day) |
+---------------------------------------+
| 2000-01-11                            |
+---------------------------------------+
1 row in set (0.00 sec)
-- 当前时间加10分钟
mysql> select date_add(now(), interval 10 minute);
+-------------------------------------+
| date_add(now(), interval 10 minute) |
+-------------------------------------+
| 2023-11-25 15:42:48                 |
+-------------------------------------+
1 row in set (0.00 sec)

在日期上减去时间

-- 当前日期减10天
mysql> select date_sub(current_date(), interval 10 day);
+-------------------------------------------+
| date_sub(current_date(), interval 10 day) |
+-------------------------------------------+
| 2023-11-15                                |
+-------------------------------------------+
1 row in set (0.00 sec)

计算两个日期相差多少天

-- date1 - date2
mysql> select datediff('2023-11-24', '2023-11-25'); 
+--------------------------------------+
| datediff('2023-11-24', '2023-11-25') |
+--------------------------------------+
|                                   -1 |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> select datediff('2023-11-25', '2023-11-24');
+--------------------------------------+
| datediff('2023-11-25', '2023-11-24') |
+--------------------------------------+
|                                    1 |
+--------------------------------------+
1 row in set (0.00 sec)
-- 新中国成立至今天数
mysql> select datediff(current_date(), '1949-10-1');
+---------------------------------------+
| datediff(current_date(), '1949-10-1') |
+---------------------------------------+
|                                 27083 |
+---------------------------------------+
1 row in set (0.00 sec)
-- 单位是天
mysql> select datediff(now(), '1949-10-1 15:0:0');
+-------------------------------------+
| datediff(now(), '1949-10-1 15:0:0') |
+-------------------------------------+
|                               27083 |
+-------------------------------------+
1 row in set (0.00 sec)

当前时间

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2023-11-25 15:46:03 |
+---------------------+
1 row in set (0.00 sec)

案例:留言板

-- 创建留言板
mysql> create table msg_tb(
    -> id int unsigned primary key auto_increment,
    -> name varchar(20) not null,
    -> msg varchar(100) comment '留言信息',
    -> msg_time timestamp
    -> );
Query OK, 0 rows affected (0.02 sec)
-- 插入数据
mysql> insert into msg_tb(name, msg) values('杜甫', '会当临绝顶');
Query OK, 1 row affected (0.01 sec)

mysql> insert into msg_tb(name, msg) values('杜甫', '一览众山小');
Query OK, 1 row affected (0.00 sec)

mysql> select * from msg_tb;
+----+--------+-----------------+---------------------+
| id | name   | msg             | msg_time            |
+----+--------+-----------------+---------------------+
|  1 | 杜甫   | 会当临绝顶      | 2023-11-25 15:59:08 |
|  2 | 杜甫   | 一览众山小      | 2023-11-25 15:59:18 |
+----+--------+-----------------+---------------------+
2 rows in set (0.00 sec)

要求:查找两分钟以内的留言信息

-- 查找一次
mysql> select name, msg from msg_tb where date_add(msg_time, interval 2 minute) >= now();
+--------+-----------------+
| name   | msg             |
+--------+-----------------+
| 杜甫   | 会当临绝顶      |
| 杜甫   | 一览众山小      |
+--------+-----------------+
2 rows in set (0.00 sec)
-- 再查找一次
mysql> select name, msg from msg_tb where date_add(msg_time, interval 2 minute) >= now();
+--------+-----------------+
| name   | msg             |
+--------+-----------------+
| 杜甫   | 会当临绝顶      |
| 杜甫   | 一览众山小      |
+--------+-----------------+
2 rows in set (0.00 sec)
-- 两分钟之后
mysql> select name, msg from msg_tb where date_add(msg_time, interval 2 minute) >= now();
Empty set (0.00 sec)



二、字符串函数

函数名称描述
charset(str)返回字符串字符集
concat(str1[, …])连接字符串
instr(str, substr)返回substr在str中的位置,位置从1开始,没有返回0
ucase(str) upper(str)转换成大写
lcase(str) lower(str)转换成小写
left(str, length) right(str, length)从str左/右边取length个字符
length(str)str的长度
replace(str, search_str, replace_str)在str中用replace_str替换search_str
strcmp(str1, str2)逐字符比较两字符串大小
substring(str, pos[, length])从str的pos位置取length个字符,默认取到结尾
ltrim(str) rtrim(str) trim(str)去掉前空格,后空格或者两边空格

查看字符串字符集

mysql> select charset('aaa');
+----------------+
| charset('aaa') |
+----------------+
| utf8           |   -- utf8编码
+----------------+
1 row in set (0.00 sec)

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

mysql> select charset(123);
+--------------+
| charset(123) |
+--------------+
| binary       | -- 二进制编码
+--------------+
1 row in set (0.00 sec)

字符串连接

-- 两个字符串连接
mysql> select concat('a', 'b');
+------------------+
| concat('a', 'b') |
+------------------+
| ab               |
+------------------+
1 row in set (0.00 sec)
-- 多个字符串连接
mysql> select concat('a', 'b', 'c');
+-----------------------+
| concat('a', 'b', 'c') |
+-----------------------+
| abc                   |
+-----------------------+
1 row in set (0.00 sec)
-- 数字转换为字符串进行拼接
mysql> select concat('a', 'b', 'c', 1234);
+-----------------------------+
| concat('a', 'b', 'c', 1234) |
+-----------------------------+
| abc1234                     |
+-----------------------------+
1 row in set (0.00 sec)

查找字符串

-- 位置从1开始
mysql> select instr('abcd123efg', 'abc');
+----------------------------+
| instr('abcd123efg', 'abc') |
+----------------------------+
|                          1 |
+----------------------------+
1 row in set (0.00 sec)
-- 不存在返回0
mysql> select instr('abcd123efg', 'aaa');
+----------------------------+
| instr('abcd123efg', 'aaa') |
+----------------------------+
|                          0 |
+----------------------------+
1 row in set (0.00 sec)

大小写转换

-- 字符串转大写
mysql> select ucase('abcD');
+---------------+
| ucase('abcD') |
+---------------+
| ABCD          |
+---------------+
1 row in set (0.00 sec)
-- 字符串转小写
mysql> select lcase('ABCD');
+---------------+
| lcase('ABCD') |
+---------------+
| abcd          |
+---------------+
1 row in set (0.00 sec)
-- 字符串转大写
mysql> select upper('hello word');
+---------------------+
| upper('hello word') |
+---------------------+
| HELLO WORD          |
+---------------------+
1 row in set (0.00 sec)
-- 字符串转小写
mysql> select lower('ABCdefG');
+------------------+
| lower('ABCdefG') |
+------------------+
| abcdefg          |
+------------------+
1 row in set (0.00 sec)

子串提取

-- 从左边开始提取3个字符
mysql> select left('abcdefghhh3', 3);
+------------------------+
| left('abcdefghhh3', 3) |
+------------------------+
| abc                    |
+------------------------+
1 row in set (0.00 sec)
-- 从左边开始提取7个字符
mysql> select left('abcdefghhh3', 7);
+------------------------+
| left('abcdefghhh3', 7) |
+------------------------+
| abcdefg                |
+------------------------+
1 row in set (0.00 sec)
-- 从右边开始提取3个字符
mysql> select right('abcdefghhh3', 3);
+-------------------------+
| right('abcdefghhh3', 3) |
+-------------------------+
| hh3                     |
+-------------------------+
1 row in set (0.00 sec)
-- 从位置3开始提取到结尾
mysql> select substring('abcdefghhh3', 3);
+-----------------------------+
| substring('abcdefghhh3', 3) |
+-----------------------------+
| cdefghhh3                   |
+-----------------------------+
1 row in set (0.00 sec)
-- 从位置1开始提取到结尾
mysql> select substring('abcdefghhh3', 1);
+-----------------------------+
| substring('abcdefghhh3', 1) |
+-----------------------------+
| abcdefghhh3                 |
+-----------------------------+
1 row in set (0.00 sec)
-- 从位置1开始提取3个字符
mysql> select substring('abcdefghhh3', 1, 3);
+--------------------------------+
| substring('abcdefghhh3', 1, 3) |
+--------------------------------+
| abc                            |
+--------------------------------+
1 row in set (0.00 sec)

字符串长度

mysql> select length('abc');
+---------------+
| length('abc') |
+---------------+
|             3 |
+---------------+
1 row in set (0.00 sec)
--  该数据库采用utf8编码,utf8为变长编码集,一个英文字母占一个字节,一个汉字占三个字节
-- 注意字节和字符:一个汉字是一个字符,一个汉字占三个字节
-- length求的是字符串所占字节长度
mysql> select length('中国');
+------------------+
| length('中国')   |
+------------------+
|                6 |
+------------------+
1 row in set (0.00 sec)
-- 转换为字符串'123'求长度
mysql> select length(123);
+-------------+
| length(123) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)

字符串替换

mysql> select replace('abc def abc', 'abc', 'hahaha') as replase;
+-------------------+
| replase           |
+-------------------+
| hahaha def hahaha |
+-------------------+
1 row in set (0.00 sec)
-- 替换字符串不存在就不处理
mysql> select replace('abc def abc', 'abcdef', 'hahaha') as replase;
+-------------+
| replase     |
+-------------+
| abc def abc |
+-------------+
1 row in set (0.00 sec)

字符串比较

-- str1 = str2
mysql> select strcmp('abc', 'abc');
+----------------------+
| strcmp('abc', 'abc') |
+----------------------+
|                    0 |
+----------------------+
1 row in set (0.00 sec)
-- str1 > str2
mysql> select strcmp('abc', 'aaaa');
+-----------------------+
| strcmp('abc', 'aaaa') |
+-----------------------+
|                     1 |
+-----------------------+
1 row in set (0.00 sec)
-- str1 < str2
mysql> select strcmp('abc', 'b');
+--------------------+
| strcmp('abc', 'b') |
+--------------------+
|                 -1 |
+--------------------+
1 row in set (0.00 sec)

消除左右空格

-- 字符串
mysql> select '          a   bc       ' as str;
+-------------------------+
| str                     |
+-------------------------+
|           a   bc        |
+-------------------------+
1 row in set (0.00 sec)
-- 删去左边空格
mysql> select ltrim('          a   bc       ') as str;
+---------------+
| str           |
+---------------+
| a   bc        |
+---------------+
1 row in set (0.00 sec)
-- 删去右边空格
mysql> select rtrim('          a   bc       ') as str;
+------------------+
| str              |
+------------------+
|           a   bc |
+------------------+
1 row in set (0.00 sec)
-- 删去左右两边空格,中间不处理
mysql> select trim('          a   bc       ') as str;
+--------+
| str    |
+--------+
| a   bc |
+--------+
1 row in set (0.00 sec)

案例 - 1:姓名格式化

要求:姓名首字母大写,其他字母小写

-- 姓名数据
mysql> select * from name_tb;
+----------+
| name     |
+----------+
| lihua    |
| XiaoMing |
| ZHANGWEI |
+----------+
3 rows in set (0.00 sec)
-- 首字母拆分
mysql> select name, left(name, 1), substring(name, 1) from name_tb;
+----------+---------------+--------------------+
| name     | left(name, 1) | substring(name, 1) |
+----------+---------------+--------------------+
| lihua    | l             | lihua              |
| XiaoMing | X             | XiaoMing           |
| ZHANGWEI | Z             | ZHANGWEI           |
+----------+---------------+--------------------+
3 rows in set (0.00 sec)
-- 大小写转换
mysql> select name, ucase(left(name, 1)), lcase(substring(name, 2)) from name_tb;
+----------+----------------------+---------------------------+
| name     | ucase(left(name, 1)) | lcase(substring(name, 2)) |
+----------+----------------------+---------------------------+
| lihua    | L                    | ihua                      |
| XiaoMing | X                    | iaoming                   |
| ZHANGWEI | Z                    | hangwei                   |
+----------+----------------------+---------------------------+
3 rows in set (0.00 sec)
-- 拼接
mysql> select name, concat(ucase(left(name, 1)), lcase(substring(name, 2))) as 姓名 from name_tb;
+----------+----------+
| name     | 姓名     |
+----------+----------+
| lihua    | Lihua    |
| XiaoMing | Xiaoming |
| ZHANGWEI | Zhangwei |
+----------+----------+
3 rows in set (0.00 sec)

案例 - 2:学生成绩通知

格式:XXX同学你好,你本次考试总分:XX,语文:XX,数学:XX,英语:XX。

-- 学生数据
mysql> select * from grade;
+----+-----------+--------+---------+------+---------+
| id | name      | gander | chinese | math | english |
+----+-----------+--------+---------+------+---------+
|  1 | 齐静春    ||     134 |   98 |      56 |
|  2 | 陈平安    ||     174 |   80 |      77 |
|  3 | 魏山君    ||     176 |   98 |      90 |
|  5 | 刘羡阳    ||     140 |   90 |      45 |
|  6 | 陈迹      ||     140 |   95 |      30 |
|  7 | 郑大风    ||     150 |   95 |      30 |
|  8 | 宁姚      ||      99 |   99 |      99 |
|  9 | 陈暖树    ||      90 |   89 |      80 |
+----+-----------+--------+---------+------+---------+
8 rows in set (0.00 sec)

-- 先提取需要的信息
mysql> select name, chinese + math + english as 总分, chinese, math, english from grade;
+-----------+--------+---------+------+---------+
| name      | 总分   | chinese | math | english |
+-----------+--------+---------+------+---------+
| 齐静春    |    288 |     134 |   98 |      56 |
| 陈平安    |    331 |     174 |   80 |      77 |
| 魏山君    |    364 |     176 |   98 |      90 |
| 刘羡阳    |    275 |     140 |   90 |      45 |
| 陈迹      |    265 |     140 |   95 |      30 |
| 郑大风    |    275 |     150 |   95 |      30 |
| 宁姚      |    297 |      99 |   99 |      99 |
| 陈暖树    |    259 |      90 |   89 |      80 |
+-----------+--------+---------+------+---------+
8 rows in set (0.00 sec)
-- 使用 concat 函数拼接信息
mysql> select concat(name, '同学你好, 你本次考试总分:', chinese + math + english, ', 语文:', chinese, ', 数学:', math, ', 英语:', english, '.') as 通知 
    -> from grade;
+-------------------------------------------------------------------------------------+
| 通知                                                                                |
+-------------------------------------------------------------------------------------+
| 齐静春同学你好, 你本次考试总分:288, 语文:134, 数学:98, 英语:56.                     |
| 陈平安同学你好, 你本次考试总分:331, 语文:174, 数学:80, 英语:77.                     |
| 魏山君同学你好, 你本次考试总分:364, 语文:176, 数学:98, 英语:90.                     |
| 刘羡阳同学你好, 你本次考试总分:275, 语文:140, 数学:90, 英语:45.                     |
| 陈迹同学你好, 你本次考试总分:265, 语文:140, 数学:95, 英语:30.                       |
| 郑大风同学你好, 你本次考试总分:275, 语文:150, 数学:95, 英语:30.                     |
| 宁姚同学你好, 你本次考试总分:297, 语文:99, 数学:99, 英语:99.                        |
| 陈暖树同学你好, 你本次考试总分:259, 语文:90, 数学:89, 英语:80.                      |
+-------------------------------------------------------------------------------------+
8 rows in set (0.00 sec)



三、数学函数

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

绝对值

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

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

mysql> select abs(-10.01);
+-------------+
| abs(-10.01) |
+-------------+
|       10.01 |
+-------------+
1 row in set (0.04 sec)

进制转换

-- 十进制到二进制
mysql> select bin(2);
+--------+
| bin(2) |
+--------+
| 10     |
+--------+
1 row in set (0.00 sec)

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

mysql> select bin(15);
+---------+
| bin(15) |
+---------+
| 1111    |
+---------+
1 row in set (0.00 sec)
-- 十进制到十六进制
mysql> select hex(15);
+---------+
| hex(15) |
+---------+
| F       |
+---------+
1 row in set (0.00 sec)
-- 自定义进制转换:十进制到二进制
mysql> select conv(15, 10, 2);
+-----------------+
| conv(15, 10, 2) |
+-----------------+
| 1111            |
+-----------------+
1 row in set (0.00 sec)
-- 自定义进制转换:二进制到十进制
mysql> select conv(1111, 2, 10);
+-------------------+
| conv(1111, 2, 10) |
+-------------------+
| 15                |
+-------------------+
1 row in set (0.00 sec)

取整规则

在这里插入图片描述

-- 向上取整
mysql> select ceiling(10.2);
+---------------+
| ceiling(10.2) |
+---------------+
|            11 |
+---------------+
1 row in set (0.00 sec)
-- 向上取整
mysql> select ceiling(10.9);
+---------------+
| ceiling(10.9) |
+---------------+
|            11 |
+---------------+
1 row in set (0.00 sec)
-- 向下取整
mysql> select floor(10.2);
+-------------+
| floor(10.2) |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)
-- 向下取整
mysql> select floor(10.9);
+-------------+
| floor(10.9) |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)
-- 创建测试表
mysql> create table int_tb(
    -> num int
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into int_tb values(10), (10.2), (10.5), (10.9);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0
-- 四舍五入
mysql> select * from int_tb;
+------+
| num  |
+------+
|   10 |
|   10 |
|   11 |
|   11 |
+------+
4 rows in set (0.00 sec)

// 向0取整,比如C语言:
int num1 = 10.9; // num1 实际等于10,直接丢弃小数位
int num2 = 10.2; // num2 实际等于10

格式化小数位

-- 保留两位小数
mysql> select format(1.23456, 2);
+--------------------+
| format(1.23456, 2) |
+--------------------+
| 1.23               | -- 四舍五入
+--------------------+
1 row in set (0.00 sec)
-- 保留三位小数
mysql> select format(1.23456, 3);
+--------------------+
| format(1.23456, 3) |
+--------------------+
| 1.235              | -- 四舍五入
+--------------------+
1 row in set (0.00 sec)
-- 保留十位小数
mysql> select format(1.23456, 10);
+---------------------+
| format(1.23456, 10) |
+---------------------+
| 1.2345600000        |
+---------------------+
1 row in set (0.00 sec)

随机数

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

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

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

取模

-- 101 % 10 = 10 ... 1
mysql> select mod(101, 10);
+--------------+
| mod(101, 10) |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

案例-1:产生0 ~ 100随机数

要求:0 ~ 100的整数

-- format 函数截取整数部分
mysql> select format(rand() * 100, 0);
+-------------------------+
| format(rand() * 100, 0) |
+-------------------------+
| 34                      |
+-------------------------+
1 row in set (0.00 sec)

mysql> select format(rand() * 100, 0);
+-------------------------+
| format(rand() * 100, 0) |
+-------------------------+
| 54                      |
+-------------------------+
1 row in set (0.00 sec)
-- ceiling 向上取整
mysql> select ceiling(rand() * 100);
+-----------------------+
| ceiling(rand() * 100) |
+-----------------------+
|                    47 |
+-----------------------+
1 row in set (0.00 sec)



四、其他函数

函数名称描述
user()查询当前用户
datebase()显示当前正在使用的数据库
md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
password(str)MySQL使用该函数对用户数据进行加密
ifnull(val1, val2)如果val1为null,返回val2,否则返回val1

查询当前用户

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

查询当前正在使用的数据库

mysql> select database();
+------------+
| database() |
+------------+
| db2        |
+------------+
1 row in set (0.00 sec)
-- 使用数据库 db1
mysql> use db1;
Database changed

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

数据加密

-- 创建操作表
mysql> create table user_tb(
    -> name varchar(20),
    -> passwd varchar(32)
    -> );
Query OK, 0 rows affected (0.03 sec)
-- 插入数据
mysql> insert into user_tb(name, passwd) values('张三', '123456');
Query OK, 1 row affected (0.01 sec)

mysql> insert into user_tb(name, passwd) values('李四', '012345');
Query OK, 1 row affected (0.01 sec)
-- 如果对数据不做任何处理,用户的密码明文保存,如果公司数据库遭到攻击,用户的信息就会被轻而易举的窃取
mysql> select * from user_tb;
+--------+--------+
| name   | passwd |
+--------+--------+
| 张三   | 123456 |
| 李四   | 012345 |
+--------+--------+
2 rows in set (0.00 sec)

-- md5() 加密
mysql> update user_tb set passwd = md5(123456) where name = '张三';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user_tb;
+--------+----------------------------------+
| name   | passwd                           |
+--------+----------------------------------+
| 张三   | e10adc3949ba59abbe56e057f20f883e |
| 李四   | 012345                           |
+--------+----------------------------------+
2 rows in set (0.00 sec)
-- 根据密码进行查找
mysql> select name, passwd from user_tb where passwd = '123456';
Empty set (0.00 sec)

mysql> select name, passwd from user_tb where passwd = md5('123456');
+--------+----------------------------------+
| name   | passwd                           |
+--------+----------------------------------+
| 张三   | e10adc3949ba59abbe56e057f20f883e |
+--------+----------------------------------+
1 row in set (0.00 sec)

-- password对密码要求的更加严格:必须包含大小写字母,数字以及特殊字符
mysql> insert into user_tb values('王五', password('123456'));
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements -- 密码不符合要求

mysql> insert into user_tb values('王五', password('123456wwDD'));
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements -- 密码不符合要求

mysql> insert into user_tb values('王五', password('12345dwdAWDAW@Q#$6'));
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> insert into user_tb values('赵六', password('6666@WWdd.'));
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from user_tb;
+--------+-------------------------------------------+
| name   | passwd                                    |
+--------+-------------------------------------------+
| 张三   | e10adc3949ba59abbe56e057f20f883e          |
| 李四   | 012345                                    |
| 王五   | *67B40CCC0ED5939458DAF14EE1D77178C9615DFE |
| 赵六   | *94718C7C8D922CC41364D274CA13EEC71A67777B |
+--------+-------------------------------------------+
4 rows in set (0.00 sec)

md5与password对比:
md5和password都可以对数据进行加密,
md5加密后生成32位字符串
password加密后生成41位字符串
md5对进行加密的字符串无要求
password要求进行加密的字符串必须包含:大小写字母,数字以及特殊字符
MySQL对用户信息加密时一般都使用password

ifnull条件判断

-- str1 为null,输出str2
mysql> select ifnull(null, 123);
+-------------------+
| ifnull(null, 123) |
+-------------------+
|               123 |
+-------------------+
1 row in set (0.00 sec)
-- str1 为null,输出str2
mysql> select ifnull(null, null);
+--------------------+
| ifnull(null, null) |
+--------------------+
|               NULL |
+--------------------+
1 row in set (0.00 sec)
-- str1 不为null,输出str1
mysql> select ifnull(666, 123);
+------------------+
| ifnull(666, 123) |
+------------------+
|              666 |
+------------------+
1 row in set (0.00 sec)

类似于这样使用的C语言的三目操作符: exp1 ? exp1 : exp2;





文章来源:https://blog.csdn.net/m0_66363962/article/details/134614969
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。