To build the table sql
below
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`sex` tinyint(255) NOT NULL,
`age` int(11) NOT NULL,
`score` bigint(255) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (1, '龙傲天', 1, 16, 90);
INSERT INTO `score` VALUES (2, '赵日天', 1, 14, 58);
INSERT INTO `score` VALUES (3, '叶良辰', 1, 17, 54);
INSERT INTO `score` VALUES (4, '武媚娘', 0, 22, 72);
INSERT INTO `score` VALUES (5, '甄姬', 0, 36, 78);
INSERT INTO `score` VALUES (6, '徐胜虎', 1, 20, 98);
SET FOREIGN_KEY_CHECKS = 1;
CASE WHEN
Conditional judgment
CASE WHEN
In the statement can be SQL
woven into the determination logic statement, similar to Java
the if else
statement. It is divided into simple functions and conditional expressions
Simple function
-- 如果字段值等于预期值,则返回结果 1,否则返回结果 2
CASE 字段 WHEN 预期值 THEN 结果1 ELSE 结果2 END
Let's take a look at the specific usage through a simple example, the table score
Demand Description: In score
the table, sex = 1
for male, sex = 0
for female, converted to a query character display
SELECT `name`,
( CASE sex WHEN 0 THEN '女' ELSE '男' END ) AS sex
FROM
score
The result is as follows
Conditional expression
-- 如果该判断结果为 true,那么 CASE 语句将返回 '结果1',否则返回 '结果2',如果没有 ELSE,则返回 null
-- CASE 与 END 之间可以有多个 WHEN… THEN… ELSE 语句。END 表示 CASE 语句结束
CASE
WHEN 条件判断 THEN 结果1 ELSE 结果2
END
Let's take a look at the specific usage through a simple example, the table score
Demand Explanation: score
greater than or equal 90
to the outstanding, 80~90
as well 60~80
as passing, less than 60
is failing, with a SQL
statement to the statistics of each student's achievement level
SELECT NAME, score,
( CASE
WHEN score >= 90 THEN '优秀'
WHEN score >= 80 THEN '良好'
WHEN score >= 60 THEN '及格' ELSE '不及格' END
) AS LEVEL
FROM
score
The result is as follows
In conjunction with
CASE WHEN
Combined with aggregate functions, it can realize more complex statistical functions
Let's take a look at the specific usage through a simple example, the table score
Demand Description: In score
the table, count how many boys and girls as well as boys and how many have failed
SELECT
SUM( CASE WHEN sex = 0 THEN 1 ELSE 0 END ) AS 女生人数,
SUM( CASE WHEN sex = 1 THEN 1 ELSE 0 END ) AS 男生人数,
SUM( CASE WHEN score >= 60 AND sex = 0 THEN 1 ELSE 0 END ) 男生及格人数,
SUM( CASE WHEN score >= 60 AND sex = 1 THEN 1 ELSE 0 END ) 女生及格人数
FROM
score
The result is as follows
IF
Conditional judgment
-- expr 是一个条件表达式,如果结果为 true,则返回 result_true,否则返回 result_false
IF(expr, result_true, result_false)
Usage example
SELECT
`name`,
IF( sex = 1, '男', '女' ) AS sex
FROM
score
The result is as follows
As can be seen, in some scenarios, IF
functions, and CASE WHEN
there is the same effect, IF
the function is relatively simple and CASE WHEN
can cope with a more complex decision. In addition, IF
functions can also be combined with aggregate functions
For example, query how many boys and girls are in the class
SELECT
SUM(IF( sex = 1, 1, NULL )) AS 男生人数,
SUM(IF( sex = 0, 1, NULL )) AS 女生人数
FROM
score
The result is as follows
IF NULL
Conditional judgment
We hope SQL
to do some processing, if the query result NULL
, is converted to a specific value, which use mysql
the IF NULL
function
SQL
Generally written like this
SELECT price FROM goods WHERE name = 'light'
Use IF NULL
function redrafting
SELECT IFNULL(price, 0) price FROM goods WHERE name = 'light'
But the use of IF NULL
the statement, if the where
condition of name
the value does not exist, it will returnNULL
IF NULL
Functions combined with aggregate functions
-- 返回结果:0
SELECT IFNULL(SUM(price), 0) FROM goods WHERE status = 3
Other AVG、COUNT
like treated in the same manner, and regardless of where
the presence of absence, the result is returned 0
to