MySQL - process control function

In MySQL, a flow control function refers to a statement that can control the execution flow in a stored procedure or function. The following are several commonly used flow control functions:

1. IF function

Realize the effect of IF...ELSE....

# 如果expr1为true,则返回expr2,否则返回expr3
IF(expr1,expr2,expr3)

It can be seen that the IF function is very similar to the ternary operator, such as:

		// 比较最大数
        int a=10;
        int b=5;
        // 比较
        int bigNum=a>b?a:b;

That is, among the three parameters of the IF function, expr1 is a conditional expression, and the final result is true or false. If the condition is true (true), it returns expr2, and if the condition is not true (false), it returns expr3. example:

SELECT IF(10>5,10,5) AS bigNum;

insert image description here

2.IFNULL function

It also realizes the effect of IF...ELSE..., which is equivalent to a variant of the IF function.

# 如果expr1不为NULL,则返回expr1,否则返回expr2
IFNULL(expr1,expr2)

That is to say, the original conditional expression is transformed into judging whether expr1 is null. If it is not null, it is itself (expr1), and if it is empty, it is expr2.

SELECT IFNULL(null,'不空') AS notNull

insert image description here
Equivalent to the IF function

SELECT IF(null IS NULL,null,'不空') AS notNull

Determine whether expr1 is empty, display expr2 if it is empty (true), and display expr3 if it is not empty (false)

3. CASE function

The case function has two different effects

switch case

It is equivalent to the effect of switch case in java. That is, the value of the variable expression in the switch is compared with the constant behind the case.

		int week=3;
        switch (week){
    
    
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期日");
                break;
            default:
                System.out.println("非法数据");
                break;
        }

Expressed in sql:

# NOW()函数用于获取当前日期和时间,
# WEEKDAY(date)函数,表示返回date对应的工作日索引,
# 因为索引从0开始,所以加1;也可以不加1,将when的常量改为工作日索引也可
SELECT CASE WEEKDAY(NOW())+1
	WHEN 1 THEN '星期一'
	WHEN 2 THEN '星期二'
    WHEN 3 THEN '星期三'
    WHEN 4 THEN '星期四'
    WHEN 5 THEN '星期五'
    WHEN 6 THEN '星期六'
    WHEN 7 THEN '星期日'	
	ELSE '非法数据'
END AS `week`;

insert image description here
function structure

case  要判断的变量(字段)或表达式
when  常量1  then  要显示的值1(或语句1;
when  常量2  then  要显示的值2(或语句2;
.......
else  要显示的值n或语句n;
end

注意:

  1. Unlike java, write case directly instead of switch, and there are no curly braces
  2. Add a constant value directly after when, instead of writing a colon, use then
  3. If it is displayed after then , there is no need to add a semicolon; if it is added after then 语句, a semicolon is required.
  4. There can be multiple when...then...statements.
  5. The default is else.
  6. end with end

Multiple IF

Similar to multiple if judgments in java.

		int grade=87;
        if (grade>=90){
    
    
            System.out.println("优秀");
        }else if (grade>=80){
    
    
            System.out.println("良好");
        }else if (grade>=70){
    
    
            System.out.println("一般");
        }else if (grade>=60){
    
    
            System.out.println("及格");
        }else {
    
    
            System.out.println("不及格");
        }

Expressed in sql:

SELECT id,`name`,chinese,
	CASE
		WHEN chinese>=90 THEN '优秀'
		WHEN chinese>=80 THEN '良好'
		WHEN chinese>=70 THEN '一般'
		WHEN chinese>=60 THEN '及格'
		ELSE '不及格'
	END AS `rank`
FROM student

insert image description here
Function structure:

case
when  条件1  then  要显示的值1或语句1;
when  条件2  then  要显示的值2或语句2;
......
else  要显示的值n或语句n
end

注意:

  1. There is no condition after the case
  2. When is followed by a condition, the result is true or false; if the condition is met, then is executed, and the following value or statement is displayed
  3. If it is displayed after the same then , there is no need to add a semicolon; if it is followed by then 语句, a semicolon is required.

Guess you like

Origin blog.csdn.net/qq_41596778/article/details/130282443