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

【MySQL内置函数】

目录:

  • 前言
  • 一、日期函数
      • 获取日期
      • 获取时间
      • 获取时间戳
      • 在日期上增加时间
      • 在日期上减去时间
      • 计算两个日期相差多少天
      • 当前时间
      • 案例:留言板
  • 二、字符串函数
      • 查看字符串字符集
      • 字符串连接
      • 查找字符串
      • 大小写转换
      • 子串提取
      • 字符串长度
      • 字符串替换
      • 字符串比较
      • 消除左右空格
      • 案例 - 1:姓名格式化
      • 案例 - 2:学生成绩通知
  • 三、数学函数
      • 绝对值
      • 进制转换
      • 取整规则
      • 格式化小数位
      • 随机数
      • 取模
      • 案例-1:产生0 ~ 100随机数
  • 四、其他函数
      • 查询当前用户
      • 查询当前正在使用的数据库
      • 数据加密
      • ifnull条件判断

前言

剑指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 changedmysql> 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: 0mysql> 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;





相关文章:

【MySQL内置函数】

目录&#xff1a; 前言一、日期函数获取日期获取时间获取时间戳在日期上增加时间在日期上减去时间计算两个日期相差多少天当前时间案例&#xff1a;留言板 二、字符串函数查看字符串字符集字符串连接查找字符串大小写转换子串提取字符串长度字符串替换字符串比较消除左右空格案…...

C++相关闲碎记录(14)

1、数值算法 &#xff08;1&#xff09;运算后产生结果accumulate() #include "algostuff.hpp"using namespace std;int main() {vector<int> coll;INSERT_ELEMENTS(coll, 1, 9);PRINT_ELEMENTS(coll);cout << "sum: " << accumulate(…...

18、vue3(十八):菜单权限,按钮权限,打包,发布nginx

目录 一、菜单权限和路由拆分 1.思路分析 2.深拷贝插件 3.代码实现 4.效果展示...

04 在Vue3中使用setup语法糖

概述 Starting from Vue 3.0, Vue introduces a new syntactic sugar setup attribute for the <script> tag. This attribute allows you to write code using Composition API (which we will discuss further in Chapter 5, The Composition API) in SFCs and shorte…...

vite+ts——user.ts——ts接口定义+axios请求的写法

import axios from axios; import qs from query-string; import {UserState} from /store/modules/user/types;export interface LoginData{username:string;password:string;grant_type?:string;scope?:string;client_id?:string;client_secret?:string;response_type?:…...

环境搭建及源码运行_java环境搭建_mysql安装

书到用时方恨少、觉知此时要躬行&#xff1b;拥有技术&#xff0c;成就未来&#xff0c;抖音视频教学地址&#xff1a;​​​​​​​​​​​​​​ 1、介绍 MySQL是一个关系型数据库管理系统&#xff0c;由瑞典MySQL AB 公司开发&#xff0c;属于 Oracle旗下产品。MySQL是最…...

Android camera的metadata

一、实现 先看一下metadata内部是什么样子&#xff1a; 可以看出&#xff0c;metadata 内部是一块连续的内存空间。 其内存分布大致可概括为&#xff1a; 区域一 &#xff1a;存 camera_metadata_t 结构体定义&#xff0c;占用内存 96 Byte 区域二 &#xff1a;保留区&#x…...

ElasticSearch面试题

1.介绍下es的架构&#xff1f; es采用的是分布式的架构&#xff0c;es集群中会有多个结点&#xff0c;而结点的角色主要有下面几种。 协调结点&#xff1a; 请求路由能力&#xff0c;将请求内容将请求转发给对应的结点进行处理。 master结点&#xff1a; 结点管理&#xff…...

C++ 数据结构知识点合集-C/C++ 数组允许定义可存储相同类型数据项的变量-供大家学习研究参考

#include <iostream> #include <cstring>using namespace std;// 声明一个结构体类型 Books struct Books {char title[50];char author[50];char subject[100];int book_id; };int main( ) {Books Book1; // 定义结构体类型 Books 的变量 Book1Books …...

【机器学习】5分钟掌握机器学习算法线上部署方法

5分钟掌握机器学习算法线上部署方法 1. 三种情况2. 如何转换PMML,并封装PMML2.1 什么是PMML2.2 PMML的使用方法范例3. 各个算法工具的工程实践4. 只用Linux的Shell来调度模型的实现方法5. 注意事项参考资料本文介绍业务模型的上线流程。首先在训练模型的工具上,一般三个模型训…...

Vue3-21-组件-子组件给父组件发送事件

情景描述 【子组件】中有一个按钮&#xff0c;点击按钮&#xff0c;触发一个事件&#xff0c; 我们希望这个事件的处理逻辑是&#xff0c;给【父组件】发送一条消息过去&#xff0c; 从而实现 【子组件】给【父组件】通信的效果。这个问题的解决就是 “发送事件” 这个操作。 …...

[密码学]AES

advanced encryption standard&#xff0c;又名rijndael密码&#xff0c;为两位比利时数学家的名字组合。 分组为128bit&#xff0c;密钥为128/192/256bit可选&#xff0c;对应加密轮数10/12/14轮。 基本操作为四种&#xff1a; 字节代换&#xff08;subBytes transformatio…...

CentOS 7 部署pure-ftp

文章目录 &#xff08;1&#xff09;简介&#xff08;2&#xff09;准备工作&#xff08;3&#xff09;更新系统&#xff08;4&#xff09;安装依赖环境&#xff08;5&#xff09;下载和解压pure-ftp源码包&#xff08;6&#xff09;编译和安装pure-ftp&#xff08;7&#xff0…...

Vue2-动态组件案例

1.component介绍 说明&#xff1a; Type: string | ComponentDefinition | ComponentConstructor Explanation: String: 如果你传递一个字符串给 is&#xff0c;它会被视为组件的名称&#xff0c;用于动态地渲染不同类型的组件。这是一个在运行时动态切换组件类型的常见用例。…...

【源码】车牌检测+QT界面+附带数据库

目录 1、基本介绍2、基本环境3、核心代码3.1、车牌识别3.2、车牌定位3.3、车牌坐标矫正 4、界面展示4.1、主界面4.2、车牌检测4.3、查询功能 5、演示6、链接 1、基本介绍 本项目采用tensorflow&#xff0c;opencv&#xff0c;pyside6和pymql编写&#xff0c;pyside6用来编写UI界…...

实战1-python爬取安全客新闻

一般步骤&#xff1a;确定网站--搭建关系--发送请求--接受响应--筛选数据--保存本地 1.拿到网站首先要查看我们要爬取的目录是否被允许 一般网站都会议/robots.txt目录&#xff0c;告诉你哪些地址可爬&#xff0c;哪些不可爬&#xff0c;以安全客为例子 2. 首先测试在不登录的…...

光栅化渲染:可见性问题和深度缓冲区算法

在前面第二章中&#xff0c;我们了解到&#xff0c;在投影点&#xff08;屏幕空间中的点&#xff09;的第三个坐标中&#xff0c;我们存储原始顶点 z 坐标&#xff08;相机空间中点的 z 坐标&#xff09;&#xff1a; 当一个像素与多个三角形重叠时&#xff0c;查找三角形表面上…...

docker入门小结

docker是什么&#xff1f;它有什么优势&#xff1f; 快速获取开箱即用的程序 docker使得所有的应用传输就像我们日常通过聊天工具文件传输一样&#xff0c;发送方将程序传输到超级码头而接收方也只需通过超级码头进行获取即可&#xff0c;就像一只鲸鱼拖着货物来回运输一样。…...

LLM Agent发展演进历史(观看metagpt视频笔记)

LLM相关的6篇重要的论文&#xff0c;其中4篇来自谷歌&#xff0c;2篇来自openai。技术路径演进大致是&#xff1a;SSL (Self-Supervised Learning) -> SFT (Supervised FineTune) IT (Instruction Tuning) -> RLHF。 word embedding的问题&#xff1a;新词如何处理&…...

Linux(操作系统)面经——part2

1、请你说说进程和线程的区别 1.进程是操作系统资源分配和调度的最小单位&#xff0c;实现操作系统内部的并发&#xff1b;线程是进程的子任务&#xff0c;cpu可以识别、执行的最小单位&#xff0c;实现程序内部的并发。 2.一个进程最少有一个线程或有多个&#xff0c;一个线程…...

应用升级/灾备测试时使用guarantee 闪回点迅速回退

1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间&#xff0c; 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点&#xff0c;不需要开启数据库闪回。…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

pikachu靶场通关笔记22-1 SQL注入05-1-insert注入(报错法)

目录 一、SQL注入 二、insert注入 三、报错型注入 四、updatexml函数 五、源码审计 六、insert渗透实战 1、渗透准备 2、获取数据库名database 3、获取表名table 4、获取列名column 5、获取字段 本系列为通过《pikachu靶场通关笔记》的SQL注入关卡(共10关&#xff0…...

Web 架构之 CDN 加速原理与落地实践

文章目录 一、思维导图二、正文内容&#xff08;一&#xff09;CDN 基础概念1. 定义2. 组成部分 &#xff08;二&#xff09;CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 &#xff08;三&#xff09;CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 &#xf…...

Typeerror: cannot read properties of undefined (reading ‘XXX‘)

最近需要在离线机器上运行软件&#xff0c;所以得把软件用docker打包起来&#xff0c;大部分功能都没问题&#xff0c;出了一个奇怪的事情。同样的代码&#xff0c;在本机上用vscode可以运行起来&#xff0c;但是打包之后在docker里出现了问题。使用的是dialog组件&#xff0c;…...

【Go语言基础【13】】函数、闭包、方法

文章目录 零、概述一、函数基础1、函数基础概念2、参数传递机制3、返回值特性3.1. 多返回值3.2. 命名返回值3.3. 错误处理 二、函数类型与高阶函数1. 函数类型定义2. 高阶函数&#xff08;函数作为参数、返回值&#xff09; 三、匿名函数与闭包1. 匿名函数&#xff08;Lambda函…...

JavaScript基础-API 和 Web API

在学习JavaScript的过程中&#xff0c;理解API&#xff08;应用程序接口&#xff09;和Web API的概念及其应用是非常重要的。这些工具极大地扩展了JavaScript的功能&#xff0c;使得开发者能够创建出功能丰富、交互性强的Web应用程序。本文将深入探讨JavaScript中的API与Web AP…...

MFC 抛体运动模拟:常见问题解决与界面美化

在 MFC 中开发抛体运动模拟程序时,我们常遇到 轨迹残留、无效刷新、视觉单调、物理逻辑瑕疵 等问题。本文将针对这些痛点,详细解析原因并提供解决方案,同时兼顾界面美化,让模拟效果更专业、更高效。 问题一:历史轨迹与小球残影残留 现象 小球运动后,历史位置的 “残影”…...

C#学习第29天:表达式树(Expression Trees)

目录 什么是表达式树&#xff1f; 核心概念 1.表达式树的构建 2. 表达式树与Lambda表达式 3.解析和访问表达式树 4.动态条件查询 表达式树的优势 1.动态构建查询 2.LINQ 提供程序支持&#xff1a; 3.性能优化 4.元数据处理 5.代码转换和重写 适用场景 代码复杂性…...