当前位置:网站首页>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 .
边栏推荐
- MySQL 45 lecture learning notes (XIV) count (*)
- About how idea sets up shortcut key sets
- Vulhub vulnerability recurrence 77_ zabbix
- ABCD four sequential execution methods, extended application
- Knowledge payment applet dream vending machine V2
- List of top ten professional skills required for data science work
- Responsive - media query
- 【网络数据传输】基于FPGA的百兆网/兆网千UDP数据包收发系统开发,PC到FPGA
- CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
- Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
猜你喜欢
Set JTAG fuc invalid to normal IO port
移动适配:vw/vh
Wechat applet scroll view component scrollable view area
响应式移动Web测试题
Flink memory model, network buffer, memory tuning, troubleshooting
Vulhub vulnerability recurrence 76_ XXL-JOB
Status of the thread
How notepad++ counts words
[thread pool]
Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
随机推荐
What is the use of cloud redis? How to use cloud redis?
MySQL storage engine
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)
The final week, I split
tars源码分析之9
MySQL 45 learning notes (XI) how to index string fields
MySQL 45 lecture learning notes (XIII) delete half of the table data, and the table file size remains the same
Mysql 45讲学习笔记(十四)count(*)
Mysql 45讲学习笔记(七)行锁
Centos8 install mysql 7 unable to start up
MySQL 45 lecture learning notes (VII) line lock
在已經知道錶格列勾選一個顯示一列
Set JTAG fuc invalid to normal IO port
云Redis 有什么用? 云redis怎么用?
Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
Cochez une colonne d'affichage dans une colonne de tableau connue
《剑指Offer》第2版——力扣刷题
[MySQL transaction]
Splicing plain text into JSON strings - easy language method
The crackdown on Huawei prompted made in China to join forces to fight back, and another enterprise announced to invest 100 billion in R & D