当前位置:网站首页>Summary of MySQL common judgment functions!! Have you used it
Summary of MySQL common judgment functions!! Have you used it
2022-07-04 07:01:00 【Chen haha Ben ha】
Partners want to find out exactly what they want to see MySQL article ? here → MySQL Jianghu Road | Column catalog
Speaking of if else You must be no stranger , This kind of judgment function is common in various programming languages , But I'm writing SQL In the sentence , Maybe you rarely use , I haven't even played much yet .
stay MySQL The function based on the judgment of condition is also called “ Control flow function ”, be used for mysql Logical judgment in statements . Let's take a look at MySQL What are the common control flow functions in , And the use scenarios of control flow function ?

Catalog
- 1、 Used in update conditions of update statements
- 2、 Used in the return value of a query statement
- 3、 Used in group query statements
- Two 、 function :IF(expr,if_true_expr,if_false_expr)
- 3、 ... and 、 function :IFNULL(expr1,expr2)
- attach 、 A picture with a story ( Nine )
One 、 function :CASE WHEN … THEN … ELSE … END
stay SQL In the sentence ,"CASE WHEN … THEN … ELSE … END" Is a more common sentence used to judge , It is suitable for adding, deleting, modifying and checking all kinds of sentences , The formula is as follows :
CASE expression
WHEN if_true_expr THEN return_value1
WHEN if_true_expr THEN return_value2
WHEN if_true_expr THEN return_value3
……
ELSE default_return_value
END
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
1、 Used in update conditions of update statements
- Give me a situation 1: Women's day big feedback ,2020 New users registered in , All adult female accounts send 10 Yuan a red envelope , Other users send 5 Yuan a red envelope , Auto recharge .
The sample statements are as follows :
-- Red envelope sentence
UPDATE users_info u
SET u.balance = CASE WHEN u.sex =' Woman ' and u.age > 18 THEN u.balance + 10
ELSE u.balance + 5 end
WHERE u.create_time >= '2020-01-01'
- 1.
- 2.
- 3.
- 4.
- 5.
Something to watch out for ,Case Function returns only the first value that matches the condition , The rest Case when Parts will be automatically ignored
2、 Used in the return value of a query statement
- Give me a situation 2: There is a student's college entrance examination score table , You need to list the levels ,650 A key university with a score above ,600-650 It's a book ,500-600 There are two books ,400-500 It's three books ,400 The following College ;
The original test data are as follows :
mysql> select * from student_score;
+-----+-----------+-------------+------+
| SID | S_NAME | TOTAL_SCORE | RANK |
+-----+-----------+-------------+------+
| 1 | Chen ha ha | 385 | 1760 |
| 2 | Hu Yapeng | 491 | 1170 |
| 3 | Liu Xiaoli | 508 | 1000 |
| 5 | Xu Linan | 599 | 701 |
| 6 | Gu Hao | 601 | 664 |
| 7 | Chen Zi Ning | 680 | 9 |
| 14 | Zhu Zhipeng | 335 | 1810 |
| 19 | Leon | 550 | 766 |
+-----+-----------+-------------+------+
8 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
Query statement :
SELECT *,case when total_score >= 650 THEN ' Key universities '
when total_score >= 600 and total_score <650 THEN ' a copy '
when total_score >= 500 and total_score <600 THEN ' Two copies '
when total_score >= 400 and total_score <500 THEN ' Three copies '
else ' junior college ' end as status_student
from student_score;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
mysql> SELECT *,case when total_score >= 650 THEN ' Key universities '
-> when total_score >= 600 and total_score <650 THEN ' a copy '
-> when total_score >= 500 and total_score <600 THEN ' Two copies '
-> when total_score >= 400 and total_score <500 THEN ' Three copies '
-> else ' junior college ' end as status_student
-> from student_score;
+-----+-----------+-------------+------+----------------+
| SID | S_NAME | TOTAL_SCORE | RANK | status_student |
+-----+-----------+-------------+------+----------------+
| 1 | Chen ha ha | 385 | 1760 | junior college |
| 2 | Hu Yapeng | 491 | 1170 | Three copies |
| 3 | Liu Xiaoli | 508 | 1000 | Two copies |
| 5 | Xu Linan | 599 | 701 | Two copies |
| 6 | Gu Hao | 601 | 664 | a copy |
| 7 | Chen Zi Ning | 680 | 9 | Key universities |
| 14 | Zhu Zhipeng | 335 | 1810 | junior college |
| 19 | Leon | 550 | 766 | Two copies |
+-----+-----------+-------------+------+----------------+
8 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
3、 Used in group query statements
- Give me a situation 3: Users include all provinces and cities in China , Statistics should be made by province , Shandong Province 、 The number of users in Guangzhou and other provinces and cities ;( This is for testing use , In fact, there should be a column of attribution province or another attribution surface in the reasoning table .)
The data are as follows :
mysql> select * from users_area;
+----+--------------+-------------+
| id | city | users_count |
+----+--------------+-------------+
| 1 | Beijing | 650 |
| 2 | Shanghai | 500 |
| 3 | jinan | 300 |
| 4 | Qingdao | 100 |
| 5 | Guangzhou | 350 |
| 6 | Shenzhen | 400 |
| 7 | Zaozhuang | 120 |
| 8 | urumqi | 80 |
+----+--------------+-------------+
8 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
Group query SQL:
SELECT
SUM(c.users_count) AS ' Number of users ',
CASE c.city
WHEN ' jinan ' THEN ' Shandong Province '
WHEN ' Qingdao ' THEN ' Shandong Province '
WHEN ' Zaozhuang ' THEN ' Shandong Province '
WHEN ' Guangzhou ' THEN ' Guangdong province, '
WHEN ' Shenzhen ' THEN ' Guangdong province, '
ELSE ' other ' END AS ' Belong to the province '
FROM
users_area c
GROUP BY CASE c.city
WHEN ' jinan ' THEN ' Shandong Province '
WHEN ' Qingdao ' THEN ' Shandong Province '
WHEN ' Zaozhuang ' THEN ' Shandong Province '
WHEN ' Guangzhou ' THEN ' Guangdong province, '
WHEN ' Shenzhen ' THEN ' Guangdong province, '
ELSE ' other ' END;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
Query results :
mysql> SELECT
-> SUM(c.users_count) AS ' Number of users ',
-> CASE c.city
-> WHEN ' jinan ' THEN ' Shandong Province '
-> WHEN ' Qingdao ' THEN ' Shandong Province '
-> WHEN ' Zaozhuang ' THEN ' Shandong Province '
-> WHEN ' Guangzhou ' THEN ' Guangdong province, '
-> WHEN ' Shenzhen ' THEN ' Guangdong province, '
-> ELSE ' other ' END AS ' Belong to the province '
-> FROM
-> users_area c
-> GROUP BY CASE c.city
-> WHEN ' jinan ' THEN ' Shandong Province '
-> WHEN ' Qingdao ' THEN ' Shandong Province '
-> WHEN ' Zaozhuang ' THEN ' Shandong Province '
-> WHEN ' Guangzhou ' THEN ' Guangdong province, '
-> WHEN ' Shenzhen ' THEN ' Guangdong province, '
-> ELSE ' other ' END;
+--------------+-----------+
| Number of users | Belong to the province |
+--------------+-----------+
| 1230 | other |
| 520 | Shandong Province |
| 750 | Guangdong province, |
+--------------+-----------+
3 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
Two 、 function :IF(expr,if_true_expr,if_false_expr)
stay mysql in if() The usage of the function is similar to java Binocular expression in , The specific syntax is as follows :
IF(expr,if_true_expr,if_false_expr), If expr The value of is true, Then return to if_true_expr Value , If expr The value of is false, Then return to if_false_expr Value .
- Use scenarios 1:IF Function is usually used for columns where real data is replaced ; Such as gender , We usually use tinyint Storage , male = 1, Woman = 2; If the query needs to be converted into characters , This scenario applies to IF function .
The original data :
mysql> select * from student;
+----+-----------+-----+---------+-----------+
| ID | NAME | SEX | GRADE | HOBBY |
+----+-----------+-----+---------+-----------+
| 1 | Chen ha ha | 1 | 9 grade | surf the internet |
| 2 | Hu Yapeng | 1 | 9 grade | food |
| 3 | Liu Xiaoli | 2 | 9 grade | Jinxiche |
| 5 | Xu Linan | 2 | 9 grade | read |
| 6 | Gu Hao | 1 | 9 grade | Basketball |
| 7 | Chen Zi Ning | 2 | 9 grade | See a movie |
| 14 | Zhu Zhipeng | 1 | 9 grade | Read novels |
| 15 | Jia Xu | 1 | 9 grade | Brag makes you |
| 19 | Leon | 1 | 9 grade | Watch the movie |
+----+-----------+-----+---------+-----------+
9 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
Handle sex Fields are displayed in character format ;
mysql> SELECT `NAME`,IF(sex = 1,' male ',' Woman ') FROM student;
+-----------+-------------------------+
| NAME | IF(sex = 1,' male ',' Woman ') |
+-----------+-------------------------+
| Chen ha ha | male |
| Hu Yapeng | male |
| Liu Xiaoli | Woman |
| Xu Linan | Woman |
| Gu Hao | male |
| Chen Zi Ning | Woman |
| Zhu Zhipeng | male |
| Jia Xu | male |
| Leon | male |
+-----------+-------------------------+
9 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
If you will (1,2) Format data changed to (‘ male ’,‘ Woman ’) It can also be done through IF Function modification ( Remember to change the column type first ),SQL as follows :
mysql> UPDATE student set sex = IF(sex = 1,' male ',' Woman ');
Query OK, 9 rows affected (0.06 sec)
Rows matched: 9 Changed: 9 Warnings: 0
- 1.
- 2.
- 3.
Modified data :
mysql> select * from student;
+----+-----------+-----+---------+-----------+
| ID | NAME | SEX | GRADE | HOBBY |
+----+-----------+-----+---------+-----------+
| 1 | Chen ha ha | male | 9 grade | surf the internet |
| 2 | Hu Yapeng | male | 9 grade | food |
| 3 | Liu Xiaoli | Woman | 9 grade | Jinxiche |
| 5 | Xu Linan | Woman | 9 grade | read |
| 6 | Gu Hao | male | 9 grade | Basketball |
| 7 | Chen Zi Ning | Woman | 9 grade | See a movie |
| 14 | Zhu Zhipeng | male | 9 grade | Read novels |
| 15 | Jia Xu | male | 9 grade | Brag makes you |
| 19 | Leon | male | 9 grade | Watch the movie |
+----+-----------+-----+---------+-----------+
9 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- Use scenarios 2: Use the class chart above , Look up the total number of boys and girls ;SQL as follows :
(sex=' male ’ Return 1, And then use SUM Add up to get the number of boys , Girls are the same .)
SELECT SUM(IF(sex = ' male ',1,0)) as boyNum, SUM(IF(sex = ' Woman ',1,0)) as girlNum from student;
- 1.
mysql> SELECT SUM(IF(sex = ' male ',1,0)) as boyNum,SUM(IF(sex = ' Woman ',1,0)) as girlNum from student;
+--------+---------+
| boyNum | girlNum |
+--------+---------+
| 6 | 3 |
+--------+---------+
1 row in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
3、 ... and 、 function :IFNULL(expr1,expr2)
IFNULL The function is MySQL One of the control flow functions , It has two parameters , Two parameters can be real values or expressions , If expr1 No NULL, Then return the first parameter (expr1). otherwise ,IFNULL The function returns the second argument .
Raw data :
mysql> select * from student;
+----+-----------+------+---------+-----------+
| ID | NAME | SEX | GRADE | HOBBY |
+----+-----------+------+---------+-----------+
| 1 | Chen ha ha | male | 9 grade | surf the internet |
| 2 | Hu Yapeng | male | 9 grade | food |
| 3 | Liu Xiaoli | Woman | 9 grade | Jinxiche |
| 5 | Xu Linan | Woman | 9 grade | read |
| 6 | Gu Hao | male | 9 grade | Basketball |
| 7 | Chen Zi Ning | Woman | 9 grade | See a movie |
| 14 | Zhu Zhipeng | NULL | 9 grade | Read novels |
| 19 | Leon | NULL | 9 grade | Watch the movie |
+----+-----------+------+---------+-----------+
8 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
take SEX by NULL The data of the project is shown as :‘ Unknown ’:
mysql> SELECT `NAME`,IFNULL(sex,' Unknown ') from student;
+-----------+----------------------+
| NAME | IFNULL(sex,' Unknown ') |
+-----------+----------------------+
| Chen ha ha | male |
| Hu Yapeng | male |
| Liu Xiaoli | Woman |
| Xu Linan | Woman |
| Gu Hao | male |
| Chen Zi Ning | Woman |
| Zhu Zhipeng | Unknown |
| Leon | Unknown |
+-----------+----------------------+
8 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
attach 、 A picture with a story ( Nine )

This picture is from 08 In Wenchuan ;
The man who lost his wife in the earthquake is full of deep feelings ,
I can't bear to leave my wife in the wild ,
Bind his body to himself ,
Take her on a motorcycle to the local morgue .
In the agony of great grief ,
He's still trying to give his wife some dignity after death .
边栏推荐
- Tar source code analysis 4
- List of top ten professional skills required for data science work
- [network data transmission] FPGA based development of 100M / Gigabit UDP packet sending and receiving system, PC to FPGA
- Node connection MySQL access denied for user 'root' @ 'localhost' (using password: yes
- Uniapp applet subcontracting
- Explain in one sentence what social proof is
- What is the "relative dilemma" in cognitive fallacy?
- Vulhub vulnerability recurrence 76_ XXL-JOB
- 【FPGA教程案例8】基于verilog的分频器设计与实现
- What is the sheji principle?
猜你喜欢

How notepad++ counts words
![[network data transmission] FPGA based development of 100M / Gigabit UDP packet sending and receiving system, PC to FPGA](/img/71/1d6179921ae84b1ba61ed094e592ff.png)
[network data transmission] FPGA based development of 100M / Gigabit UDP packet sending and receiving system, PC to FPGA

用于压缩视频感知增强的多目标网络自适应时空融合

Pangu open source: multi support and promotion, the wave of chip industry

Industrial computer anti-virus

About how idea sets up shortcut key sets

NLP-文献阅读总结

Centos8 install mysql 7 unable to start up

Status of the thread

响应式移动Web测试题
随机推荐
Wechat applet scroll view component scrollable view area
Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
Tar source code analysis Part 7
移动适配:vw/vh
关于IDEA如何设置快捷键集
What is tweeman's law?
MySQL 45 lecture learning notes (XIV) count (*)
Knowledge payment applet dream vending machine V2
Campus network problems
tars源码分析之1
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)
MySQL 45 lecture learning notes (XIII) delete half of the table data, and the table file size remains the same
Selection (023) - what are the three stages of event propagation?
Mysql 45讲学习笔记(六)全局锁
Su Weijie, a member of Qingyuan Association and an assistant professor at the University of Pennsylvania, won the first Siam Youth Award for data science, focusing on privacy data protection, etc
Vulhub vulnerability recurrence 76_ XXL-JOB
CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
Can the out of sequence message complete TCP three handshakes
Pangu open source: multi support and promotion, the wave of chip industry
《国民经济行业分类GB/T 4754—2017》官网下载地址