case when, if else and ifnull in mysql

Mysql if can be used as an expression or as a flow control statement in a stored procedure. The following is used as an expression:

IF expression


IF(expr1,expr2,expr3)


If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL), the return value of IF() is expr2; otherwise, the return value is expr3. The return value of IF() is a numeric value or a string value, depending on its context.

Copy the code code as follows:


select *,if(sva=1,"男","女") as ssva from taname where sva != ""


The if as an expression can also be implemented with CASE when:

Copy the code code as follows:


select CASE sva WHEN 1 THEN '男' ELSE '女' END as ssva from taname where sva != ''


In the return result of the first scheme, value=compare-value. The return result of the second solution is the true result of the first case. If there is no matching result value, the return result is the result after ELSE, if there is no ELSE part, the return value is NULL.

 

E.g:

code show as below:


SELECT CASE 1 WHEN 1 THEN 'one'
  WHEN 2 THEN 'two' 
   ELSE 'more' END
as testCol


Will output one

 

IFNULL(expr1,expr2)

If expr1 is not NULL, the return value of IFNULL() is expr1; otherwise, the return value is expr2. The return value of IFNULL() is a number or a string, depending on the context in which it is used.

code show as below:


mysql> SELECT IFNULL(1,0);
        -> 1

 

mysql> SELECT IFNULL(NULL,10);
        -> 10

mysql> SELECT IFNULL(1/0,10);
        -> 10

mysql> SELECT IFNULL(1/0,'yes');
        -> 'yes'


The default result of IFNULL(expr1,expr2) is the more "common" of the two expressions, in the order of STRING, REAL, or INTEGER.

 

IF ELSE as a process control statement in the use of stored procedures

If realizes conditional judgment, and performs different operations when meeting different conditions. As long as we learn programming, we all know the role of if. Let's take a look at how the if in the mysql stored procedure is used.

code show as below:


IF search_condition THEN 
    statement_list  
[ELSEIF search_condition THEN]  
    statement_list ...  
[ELSE 
    statement_list]  
END IF 


Similar to the IF statement in PHP, when the condition search_condition in the IF is established, the statement_list statement after THEN is executed, otherwise the condition in ELSEIF is judged, and the subsequent statement_list statement is executed if it is satisfied, otherwise, it continues to judge other branches. When the conditions of all branches are not met, the ELSE branch is executed. search_condition is a conditional expression, which can be composed of conditional operators such as "=, <, <=, >, >=,!=", and multiple expressions can be combined using AND, OR, and NOT.

 

For example, create a stored procedure that queries the grade (grade) of the student's student ID (student_no) and course number (course_no), and returns the grades and grades. The grades with scores greater than 90 are A-level, and those with a score of less than 90 are greater than Those equal to 80 points are grade B, those with less than 80 points and greater than 70 points are grade C, and then to grade E. Then, the code to create a stored procedure is as follows:

Copy the code code as follows:


create procedure dbname.proc_getGrade  
(stu_no varchar(20),cour_no varchar(10))  
BEGIN 
declare stu_grade float;  
select grade into stu_grade from grade where student_no=stu_no and course_no=cour_no;  
if stu_grade>=90 then 
    select stu_grade,'A';  
elseif stu_grade<90 and stu_grade>=80 then 
    select stu_grade,'B';  
elseif stu_grade<80 and stu_grade>=70 then 
    select stu_grade,'C';  
elseif stu_grade70 and stu_grade>=60 then 
    select stu_grade,'D';  
else 
    select stu_grade,'E';  
end if;  
END 


Note: IF is a statement, you need to add a semicolon ";" after END IF to indicate the end of the statement. Other statements such as CASE, LOOP, etc. are also the same.

Guess you like

Origin blog.csdn.net/JineD/article/details/111918411