mysql condition judgment function

To build the table sqlbelow

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 WHENIn the statement can be SQLwoven into the determination logic statement, similar to Javathe if elsestatement. 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 scorethe table, sex = 1for male, sex = 0for 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

Insert picture description here

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: scoregreater than or equal 90to the outstanding, 80~90as well 60~80as passing, less than 60is failing, with a SQLstatement 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

Insert picture description here

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 scorethe 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

Insert picture description here

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

Insert picture description here
As can be seen, in some scenarios, IFfunctions, and CASE WHENthere is the same effect, IFthe function is relatively simple and CASE WHENcan cope with a more complex decision. In addition, IFfunctions 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

Insert picture description here

IF NULL Conditional judgment

We hope SQLto do some processing, if the query result NULL, is converted to a specific value, which use mysqlthe IF NULLfunction

SQLGenerally written like this

SELECT price FROM goods WHERE name = 'light'

Use IF NULLfunction redrafting

SELECT IFNULL(price, 0) price FROM goods WHERE name = 'light'

But the use of IF NULLthe statement, if the wherecondition of namethe 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、COUNTlike treated in the same manner, and regardless of wherethe presence of absence, the result is returned 0to

Guess you like

Origin blog.csdn.net/weixin_38192427/article/details/115190457