当前位置:网站首页>Tutorial on principles and applications of database system (056) -- MySQL query (18): usage of other types of functions
Tutorial on principles and applications of database system (056) -- MySQL query (18): usage of other types of functions
2022-07-26 01:10:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (056)—— MySQL Inquire about ( eighteen ): Usage of other types of functions
Catalog
One 、 Judge class functions
1、IF() function
Different expression results are returned according to different values of logical expressions .
The syntax is as follows :
-- If Logical expression The result is true , return The result expression 1 Value , If Logical expression The result is false , Then return to The result expression 2 Value
IF( Logical expression , The result expression 1, The result expression 2)
for example :
(1) Examples of basic usage
mysql> select if(mod(100,2) = 0,' even numbers ',' Odd number ') from dual;
+--------------------------------------+
| if(mod(100,2) = 0,' even numbers ',' Odd number ') |
+--------------------------------------+
| even numbers |
+--------------------------------------+
1 row in set (0.00 sec)
(2) There are the following data tables , When querying salary information , If salary by NULL Is shown as 0
mysql> select * from t12;
+----+-------+--------+
| id | name | salary |
+----+-------+--------+
| 1 | Jack | 5200 |
| 2 | Tom | 4800 |
| 3 | Black | 3700 |
| 4 | Mark | NULL |
| 5 | Kate | NULL |
+----+-------+--------+
5 rows in set (0.00 sec)
mysql> select id,name,if(salary is null,0, salary) salary from t12;
+----+-------+--------+
| id | name | salary |
+----+-------+--------+
| 1 | Jack | 5200 |
| 2 | Tom | 4800 |
| 3 | Black | 3700 |
| 4 | Mark | 0 |
| 5 | Kate | 0 |
+----+-------+--------+
5 rows in set (0.00 sec)
2、IFNULL() function
Determine whether an expression is empty .
The syntax is as follows :
-- Judge expression 1 Whether the value of is empty , If it's not empty , Just go back to 【 expression 1】 Value , Otherwise return to 【 expression 2】 Value .
IFNULL( expression 1, expression 2)
for example : There are the following data tables , When querying salary information , If salary by NULL Is shown as 0
mysql> select id,name,ifnull(salary,0) salary from t12;
+----+-------+--------+
| id | name | salary |
+----+-------+--------+
| 1 | Jack | 5200 |
| 2 | Tom | 4800 |
| 3 | Black | 3700 |
| 4 | Mark | 0 |
| 5 | Kate | 0 |
+----+-------+--------+
5 rows in set (0.00 sec)
3、CASE() function
For multi branch judgment , There are two uses . Personally think that , The second usage is more flexible , It can completely replace the first usage .
The syntax is as follows :
First usage :
/* explain : (1) Judge CASE hinder " expression 0" and WHEN hinder "valuei" Whether it is equal or not . (2) If "value1" The value of and " expression 0" The values are equal , Just go back to " expression 1" Value . (3) If "value2" The value of and " expression 0" The values are equal , Just go back to " expression 2" Value , And so on . (4) If all "value" None of them is equal to " expression 0" Value , Just go back to ELSE hinder " expression n" Value . (5) If all "value" None of them is equal to " expression 0" Value , And there's no ELSE Just go back to NULL value . */
CASE expression 0
WHEN [value1] THEN expression 1
[WHEN [value2] THEN expression 2 ...]
[ELSE expression n]
END
Second usage :
/* explain : (1) Direct judgment " Logical expression i" Value , Which is true returns the corresponding " expression i" Value . (2) If all " Logical expression i" All values of are false , return ELSE hinder " expression n" Value . (3) If all " Logical expression i" All values of are false , And there's no ELSE, Then return to NULL value . */
CASE
WHEN [ Logical expression 1] THEN expression 1
[WHEN [ Logical expression 2] THEN expression 2 ...]
[ELSE expression n]
END
for example : There is the following transcript , Please check the student's grades , And display the grade of the score : good (>= 90)、 good (80 - 90)、 secondary (70 - 80)、 pass (60 - 70) And fail (< 60).
/* select s.s_id, s.s_name, c.c_id, c.c_name, sc.score from student s, score sc, course c where s.s_id = sc.s_id and c.c_id = sc.c_id; */
mysql> select s.s_id, s.s_name, c.c_id, c.c_name, sc.score
-> from student s, score sc, course c
-> where s.s_id = sc.s_id and c.c_id = sc.c_id;
+-------+-----------+------+--------------+-------+
| s_id | s_name | c_id | c_name | score |
+-------+-----------+------+--------------+-------+
| S2011 | Zhang Xiaogang | C102 | Advanced mathematics | 84 |
| S2011 | Zhang Xiaogang | C105 | Infectious diseases | 90 |
| S2012 | Liu Xiaoqing | C101 | Ancient literature | 67 |
| S2012 | Liu Xiaoqing | C102 | Advanced mathematics | 52 |
| S2012 | Liu Xiaoqing | C103 | linear algebra | 55 |
| S2012 | Liu Xiaoqing | C104 | Clinical medicine | 86 |
| S2012 | Liu Xiaoqing | C105 | Infectious diseases | 87 |
| S2013 | Cao mengde | C102 | Advanced mathematics | 97 |
| S2013 | Cao mengde | C103 | linear algebra | 68 |
| S2013 | Cao mengde | C104 | Clinical medicine | 66 |
| S2013 | Cao mengde | C105 | Infectious diseases | 68 |
| S2014 | Liu Yan | C102 | Advanced mathematics | 90 |
| S2014 | Liu Yan | C103 | linear algebra | 85 |
| S2014 | Liu Yan | C104 | Clinical medicine | 77 |
| S2014 | Liu Yan | C105 | Infectious diseases | 96 |
| S2015 | Liu Yan | C101 | Ancient literature | 69 |
| S2015 | Liu Yan | C102 | Advanced mathematics | 66 |
| S2015 | Liu Yan | C103 | linear algebra | 88 |
| S2015 | Liu Yan | C104 | Clinical medicine | 69 |
| S2015 | Liu Yan | C105 | Infectious diseases | 66 |
| S2016 | Liu Ruofei | C101 | Ancient literature | 65 |
| S2016 | Liu Ruofei | C102 | Advanced mathematics | 69 |
| S2021 | Dong Wenhua | C102 | Advanced mathematics | 72 |
| S2021 | Dong Wenhua | C103 | linear algebra | 90 |
| S2021 | Dong Wenhua | C104 | Clinical medicine | 90 |
| S2021 | Dong Wenhua | C105 | Infectious diseases | 57 |
| S2022 | Zhou Huajian | C102 | Advanced mathematics | 88 |
| S2022 | Zhou Huajian | C103 | linear algebra | 93 |
| S2023 | trump | C102 | Advanced mathematics | 68 |
| S2023 | trump | C103 | linear algebra | 86 |
| S2024 | Obama | C102 | Advanced mathematics | 87 |
| S2024 | Obama | C103 | linear algebra | 97 |
| S2025 | Zhou Jianhua | C102 | Advanced mathematics | 61 |
| S2025 | Zhou Jianhua | C105 | Infectious diseases | 62 |
| S2026 | Zhang Xueyou | C102 | Advanced mathematics | 59 |
| S2026 | Zhang Xueyou | C105 | Infectious diseases | 48 |
+-------+-----------+------+--------------+-------+
36 rows in set (0.01 sec)
(1) Use the first method to construct the query
/* select s.s_id, s.s_name, c.c_id, c.c_name, sc.score, case floor(score/10) when 9 then ' good ' when 8 then ' good ' when 7 then ' secondary ' when 6 then ' pass ' else ' fail, ' end grade from student s, score sc, course c where s.s_id = sc.s_id and c.c_id = sc.c_id; */
mysql> select s.s_id, s.s_name, c.c_id, c.c_name, sc.score,
-> case floor(score/10)
-> when 9 then ' good '
-> when 8 then ' good '
-> when 7 then ' secondary '
-> when 6 then ' pass '
-> else ' fail, '
-> end grade
-> from student s, score sc, course c
-> where s.s_id = sc.s_id and c.c_id = sc.c_id;
+-------+-----------+------+--------------+-------+-----------+
| s_id | s_name | c_id | c_name | score | grade |
+-------+-----------+------+--------------+-------+-----------+
| S2011 | Zhang Xiaogang | C102 | Advanced mathematics | 84 | good |
| S2011 | Zhang Xiaogang | C105 | Infectious diseases | 90 | good |
| S2012 | Liu Xiaoqing | C101 | Ancient literature | 67 | pass |
| S2012 | Liu Xiaoqing | C102 | Advanced mathematics | 52 | fail, |
| S2012 | Liu Xiaoqing | C103 | linear algebra | 55 | fail, |
| S2012 | Liu Xiaoqing | C104 | Clinical medicine | 86 | good |
| S2012 | Liu Xiaoqing | C105 | Infectious diseases | 87 | good |
| S2013 | Cao mengde | C102 | Advanced mathematics | 97 | good |
| S2013 | Cao mengde | C103 | linear algebra | 68 | pass |
| S2013 | Cao mengde | C104 | Clinical medicine | 66 | pass |
| S2013 | Cao mengde | C105 | Infectious diseases | 68 | pass |
| S2014 | Liu Yan | C102 | Advanced mathematics | 90 | good |
| S2014 | Liu Yan | C103 | linear algebra | 85 | good |
| S2014 | Liu Yan | C104 | Clinical medicine | 77 | secondary |
| S2014 | Liu Yan | C105 | Infectious diseases | 96 | good |
| S2015 | Liu Yan | C101 | Ancient literature | 69 | pass |
| S2015 | Liu Yan | C102 | Advanced mathematics | 66 | pass |
| S2015 | Liu Yan | C103 | linear algebra | 88 | good |
| S2015 | Liu Yan | C104 | Clinical medicine | 69 | pass |
| S2015 | Liu Yan | C105 | Infectious diseases | 66 | pass |
| S2016 | Liu Ruofei | C101 | Ancient literature | 65 | pass |
| S2016 | Liu Ruofei | C102 | Advanced mathematics | 69 | pass |
| S2021 | Dong Wenhua | C102 | Advanced mathematics | 72 | secondary |
| S2021 | Dong Wenhua | C103 | linear algebra | 90 | good |
| S2021 | Dong Wenhua | C104 | Clinical medicine | 90 | good |
| S2021 | Dong Wenhua | C105 | Infectious diseases | 57 | fail, |
| S2022 | Zhou Huajian | C102 | Advanced mathematics | 88 | good |
| S2022 | Zhou Huajian | C103 | linear algebra | 93 | good |
| S2023 | trump | C102 | Advanced mathematics | 68 | pass |
| S2023 | trump | C103 | linear algebra | 86 | good |
| S2024 | Obama | C102 | Advanced mathematics | 87 | good |
| S2024 | Obama | C103 | linear algebra | 97 | good |
| S2025 | Zhou Jianhua | C102 | Advanced mathematics | 61 | pass |
| S2025 | Zhou Jianhua | C105 | Infectious diseases | 62 | pass |
| S2026 | Zhang Xueyou | C102 | Advanced mathematics | 59 | fail, |
| S2026 | Zhang Xueyou | C105 | Infectious diseases | 48 | fail, |
+-------+-----------+------+--------------+-------+-----------+
36 rows in set (0.00 sec)
(2) Use the second method to construct queries
/* select s.s_id, s.s_name, c.c_id, c.c_name, sc.score, case when score >= 90 then ' good ' when score >= 80 then ' good ' when score >= 70 then ' secondary ' when score >= 60 then ' pass ' else ' fail, ' end grade from student s, score sc, course c where s.s_id = sc.s_id and c.c_id = sc.c_id; */
mysql> select s.s_id, s.s_name, c.c_id, c.c_name, sc.score,
-> case
-> when score >= 90 then ' good '
-> when score >= 80 then ' good '
-> when score >= 70 then ' secondary '
-> when score >= 60 then ' pass '
-> else ' fail, '
-> end grade
-> from student s, score sc, course c
-> where s.s_id = sc.s_id and c.c_id = sc.c_id;
+-------+-----------+------+--------------+-------+-----------+
| s_id | s_name | c_id | c_name | score | grade |
+-------+-----------+------+--------------+-------+-----------+
| S2011 | Zhang Xiaogang | C102 | Advanced mathematics | 84 | good |
| S2011 | Zhang Xiaogang | C105 | Infectious diseases | 90 | good |
| S2012 | Liu Xiaoqing | C101 | Ancient literature | 67 | pass |
| S2012 | Liu Xiaoqing | C102 | Advanced mathematics | 52 | fail, |
| S2012 | Liu Xiaoqing | C103 | linear algebra | 55 | fail, |
| S2012 | Liu Xiaoqing | C104 | Clinical medicine | 86 | good |
| S2012 | Liu Xiaoqing | C105 | Infectious diseases | 87 | good |
| S2013 | Cao mengde | C102 | Advanced mathematics | 97 | good |
| S2013 | Cao mengde | C103 | linear algebra | 68 | pass |
| S2013 | Cao mengde | C104 | Clinical medicine | 66 | pass |
| S2013 | Cao mengde | C105 | Infectious diseases | 68 | pass |
| S2014 | Liu Yan | C102 | Advanced mathematics | 90 | good |
| S2014 | Liu Yan | C103 | linear algebra | 85 | good |
| S2014 | Liu Yan | C104 | Clinical medicine | 77 | secondary |
| S2014 | Liu Yan | C105 | Infectious diseases | 96 | good |
| S2015 | Liu Yan | C101 | Ancient literature | 69 | pass |
| S2015 | Liu Yan | C102 | Advanced mathematics | 66 | pass |
| S2015 | Liu Yan | C103 | linear algebra | 88 | good |
| S2015 | Liu Yan | C104 | Clinical medicine | 69 | pass |
| S2015 | Liu Yan | C105 | Infectious diseases | 66 | pass |
| S2016 | Liu Ruofei | C101 | Ancient literature | 65 | pass |
| S2016 | Liu Ruofei | C102 | Advanced mathematics | 69 | pass |
| S2021 | Dong Wenhua | C102 | Advanced mathematics | 72 | secondary |
| S2021 | Dong Wenhua | C103 | linear algebra | 90 | good |
| S2021 | Dong Wenhua | C104 | Clinical medicine | 90 | good |
| S2021 | Dong Wenhua | C105 | Infectious diseases | 57 | fail, |
| S2022 | Zhou Huajian | C102 | Advanced mathematics | 88 | good |
| S2022 | Zhou Huajian | C103 | linear algebra | 93 | good |
| S2023 | trump | C102 | Advanced mathematics | 68 | pass |
| S2023 | trump | C103 | linear algebra | 86 | good |
| S2024 | Obama | C102 | Advanced mathematics | 87 | good |
| S2024 | Obama | C103 | linear algebra | 97 | good |
| S2025 | Zhou Jianhua | C102 | Advanced mathematics | 61 | pass |
| S2025 | Zhou Jianhua | C105 | Infectious diseases | 62 | pass |
| S2026 | Zhang Xueyou | C102 | Advanced mathematics | 59 | fail, |
| S2026 | Zhang Xueyou | C105 | Infectious diseases | 48 | fail, |
+-------+-----------+------+--------------+-------+-----------+
36 rows in set (0.02 sec)
Two 、 Data type conversion function
1、cast() function
Convert the data type of the expression to another data type .
The syntax is as follows :
cast( expression as data type )
2、convert() function
Convert the data type of the expression to another data type .
The syntax is as follows :
convert( expression , data type )
3、 ... and 、 System information function
1、 Return the current database name function
Returns the currently used database name .
The syntax is as follows :
DATABASE()
SCHEMA()
for example :
mysql> select DATABASE() from dual;
+------------+
| DATABASE() |
+------------+
| mydb |
+------------+
1 row in set (0.00 sec)
2、 return MySQL Version number function
return MySQL Version number of .
The syntax is as follows :
VERSION()
for example :
mysql> select VERSION() from dual;
+-----------+
| VERSION() |
+-----------+
| 5.7.25 |
+-----------+
1 row in set (0.00 sec)
3、 Query the current user information function
The following functions are used to query user information :
CURRENT_USER()
SESSION_USER()
SYSTEM_USER()
USER()
for example :
mysql> select CURRENT_USER(),SESSION_USER(),SYSTEM_USER(),USER() from dual;
+----------------+----------------+---------------+---------------+
| CURRENT_USER() | SESSION_USER() | SYSTEM_USER() | USER() |
+----------------+----------------+---------------+---------------+
| wgx@localhost | wgx@localhost | wgx@localhost | wgx@localhost |
+----------------+----------------+---------------+---------------+
1 row in set (0.00 sec)
Four 、 Encryption function
1、PASSWORD() function
Encrypt the string . In general ,PASSWORD() The function is mainly used to encrypt the user's password .
The syntax is as follows :
PASSWORD(str)
for example :
mysql> select PASSWORD('123') from dual;
+-------------------------------------------+
| PASSWORD('123') |
+-------------------------------------------+
| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
2、MD5() function
Encrypt the string .MD5() Function is mainly used to encrypt ordinary data .
The syntax is as follows :
MD5(str)
for example :
mysql> select MD5('123') from dual;
+----------------------------------+
| MD5('123') |
+----------------------------------+
| 202cb962ac59075b964b07152d234b70 |
+----------------------------------+
1 row in set (0.00 sec)
边栏推荐
- Lua基础语法
- 代理IP服务器如何保证自身在网络中的信息安全呢
- Codeforces Round #810 (Div. 2)A~C
- 【RTOS训练营】课程学习方法和C语言知识(指针、结构体、函数指针、链表)和学员问题
- Sqli-labs Less7
- 【RTOS训练营】设备子系统、晚课学员提问
- 数据库系统原理与应用教程(056)—— MySQL 查询(十八):其他类型函数的用法
- [RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions
- [RTOS training camp] equipment subsystem, questions of evening students
- [RTOS training camp] course learning methods and structural knowledge review + linked list knowledge
猜你喜欢

【RTOS训练营】关于上课和答疑

109. Upload local files using SAP ui5 fileuploader control

【RTOS训练营】任务调度(续)、任务礼让、调度总结、队列和晚课提问

Working principle of ZK rollups

Microwave oven rectifier diode cl01-12

“元气可乐”不是终点,“中国可乐”才是

ZABBIX monitoring host and resource alarm

NIO简易示例

Half of the people in the country run in Changsha. Where do half of the people in Changsha run?

IP地址能精确到哪步?动态IP及静态IP是什么?切换IP最常用的方法
随机推荐
[RTOS training camp] learn C language from a higher perspective
第二届中国Rust开发者大会来啦,完整议程大曝光!
全国一半人跑长沙,长沙一半人跑哪?
游戏思考17:寻路引擎recast和detour学习二:recast导航网格生成流程及局限性
[RTOS training camp] task scheduling (Continued), task comity, scheduling summary, queue and evening class questions
1.30 升级bin文件添加后缀及文件长度
链表相关面试题
[laser principle and application -3]: foreign brands of lasers
API测试简介
[RTOS training camp] I2C and UART knowledge and preview arrangement + evening class questions
Unity get the animation being played
How can MySQL just duplicate data?
《暗黑破坏神:不朽》手游如何多开搬砖及新手入门搬砖攻略
【RTOS训练营】设备子系统、晚课学员提问
Working principle of ZK rollups
网络性能评估工具 ping/mtr
The Chinese input (Pinyin input method) component created by lvgl official +100ask enables lvgl to support Chinese input!
Redis数据结构详解,结合书本
【RTOS训练营】继续程序框架、tick中断补充、预习、课后作业和晚课提问
109. Upload local files using SAP ui5 fileuploader control