当前位置:网站首页>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)
边栏推荐
- 【RTOS训练营】GPIO知识和预习安排 + 晚课提问
- android sqlite先分组后排序左连查询
- Lua basic grammar
- Android SQLite first groups and then sorts left concatenated queries
- 代理IP服务器如何保证自身在网络中的信息安全呢
- 力扣 25. K 个一组翻转链表
- 加载dll失败
- [secsha concept] original reverse supplement
- 【软件开发规范三】【软件版本命名规范】
- Download exclusively | Alibaba cloud maxcompute questions and answers to unlock SaaS mode cloud data warehouse in this electronic manual!
猜你喜欢

NLP introduction + practice: Chapter 3: gradient descent and back propagation

Nanjie's embarrassment

Embedded development: tips and tricks -- seven tips for designing powerful boot loader

嵌入式开发:技巧和窍门——设计强大的引导加载程序的7个技巧

Travel + strategy accelerated landing, jietu new product matrix exposure

Jupyter changes the main interface and imports the dataset
![[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions](/img/cb/de38e9f4186c4392ef8832659422ff.jpg)
[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions

The application and principle of changing IP software are very wide. Four methods of dynamic IP replacement are used to protect network privacy

【RTOS训练营】设备子系统、晚课学员提问

ASP. Net core configuration
随机推荐
It will be easier to implement MES system by doing well in these four stages
Selenium assertion and JS actuator
ZABBIX monitoring host and resource alarm
【RTOS训练营】课程学习方法和结构体知识复习 + 链表知识
Spine_附件皮肤
[laser principle and application-4]: internal structure and working principle of laser
Codeforces Round #810 (Div. 2)A~C
[Code] refers to the repeated number in the offer 03 array
How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP
【软件开发规范二】《禁止项开发规范》
Sqli-labs Less7
Web middleware log analysis script 3.0 (shell script)
SQL的CASE WHEN
Centrosymmetric binary mode cslbp, matlab
109. Upload local files using SAP ui5 fileuploader control
【RTOS训练营】环形缓冲区、AT指令、预习安排和晚课提问
力扣 25. K 个一组翻转链表
Failed to load DLL
【Code】剑指offer 03数组中重复的数字
《nlp入门+实战:第三章:梯度下降和反向传播 》