当前位置:网站首页>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 .
边栏推荐
- js 常用时间处理函数
- Mysql 45讲学习笔记(十一)字符串字段怎么加索引
- 关于IDEA如何设置快捷键集
- Cervical vertebra, beriberi
- Tar source code analysis Part 10
- 请问旧版的的常用SQL怎么迁移到新版本里来?
- tcp socket 的 recv 如何接收指定长度消息?
- Four sets of APIs for queues
- Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
- Computer connects raspberry pie remotely through putty
猜你喜欢
Selenium ide plug-in download, installation and use tutorial
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
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)
Flink memory model, network buffer, memory tuning, troubleshooting
Selenium driver ie common problem solving message: currently focused window has been closed
【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
Wechat applet scroll view component scrollable view area
Set JTAG fuc invalid to normal IO port
Vulhub vulnerability recurrence 76_ XXL-JOB
selenium IDE插件下载安装使用教程
随机推荐
Background and current situation of domestic CDN acceleration
tars源码分析之5
Responsive - media query
《国民经济行业分类GB/T 4754—2017》官网下载地址
Mysql 45讲学习笔记(六)全局锁
kubernetes集群之Label管理
Tar source code analysis 6
Knowledge payment applet dream vending machine V2
Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
Industrial computer anti-virus
Boast about Devops
centos8安装mysql.7 无法开机启动
Introduction to spark core components
在已经知道表格列勾选一个显示一列
[Valentine's day] - you can change your love and write down your lover's name
Mysql 45讲学习笔记(十一)字符串字段怎么加索引
Can the out of sequence message complete TCP three handshakes
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
The final week, I split