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;
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
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`;
function structure
case 要判断的变量(字段)或表达式
when 常量1 then 要显示的值1(或语句1);
when 常量2 then 要显示的值2(或语句2);
.......
else 要显示的值n或语句n;
end
注意:
- Unlike java, write case directly instead of switch, and there are no curly braces
- Add a constant value directly after when, instead of writing a colon, use then
- If it is displayed after then
值
, there is no need to add a semicolon; if it is added after then语句
, a semicolon is required. - There can be multiple when...then...statements.
- The default is else.
- 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
Function structure:
case
when 条件1 then 要显示的值1或语句1;
when 条件2 then 要显示的值2或语句2;
......
else 要显示的值n或语句n
end
注意:
- There is no condition after the case
- 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
- 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.