Summary of MySQL if,case statement usage

Source: Internet
Author: User
Tags case statement

MySQL can be used as an expression, or as a Process control statement in a stored procedure, as an expression:

An If expression

IF(expr1,expr2,expr3)

If Expr1 is true (expr1 <> 0 and Expr1 <> NULL), then 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 the context in which it is located.

Select*,if(sva=1,"male","female")as from where! = ""
The IF as an expression can also be implemented with case:
Select1' female 'END as fromwhere!='      

In the return result of the first scenario, Value=compare-value. The return result of the second scenario is the real result of the first case. If there is no matching result value, the result is returned with the else result, and if there is no else part, the return value is NULL.

For example:

11' one '2 ' + ' more'END as testcol       

Will output one

Ifnull (EXPR1,EXPR2)

If EXPR1 is not NULL, the return value of Ifnull () is expr1; Otherwise its return value is EXPR2. The return value of Ifnull () is either a number or a string, depending on the context in which it is used.

Mysql>SELECT Ifnull(1,0); - 1Mysql>SELECT Ifnull(Null,10); -> 10mysql>< Span class= "PLN" > SELECT ifnull (1/0,10-> 10mysql>< Span class= "PLN" > SELECT ifnull (1/0, ' yes ' ->  ' yes '          

IFNULL(expr1,expr2)The default result value is one of the more "common" in two expressions, in the order of string, real, or INTEGER.

IF ELSE is used as a Process Control statement

If the implementation of the conditions to judge, to meet different conditions to perform different operations, this we just learn programming all know if the role of if, let's look at the MySQL stored procedure if is how to use it.

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

Similar to the IF statement in PHP, when the condition in the If search_condition, execute then after the Statement_list statement, otherwise determine the conditions in the ElseIf, the establishment then executes the Statement_list statement thereafter, Otherwise continue to judge other branches. Executes the Else branch when none of the conditions of the branch are true. Search_condition is a conditional expression that can be composed of conditional operators such as =, <, <=, >, >=,! =, and multiple expressions can be combined using and, or, not.

For example, establish a stored procedure that queries its score (grade) by student number (STUDENT_NO) and course number (COURSE_NO), returns the grade of grades and grades, scores greater than 90 for Class A, less than 90 points greater than or equal to 80 points for Class B, Less than 80 points is greater than or equal to 70 points for Class C, and then to E-class. So, the code for creating the stored procedure is as follows:

CREATE PROCEDURE DBName.Proc_getgrade(Stu_no varchar(20),Cour_no varchar(10)) BEGINDECLARE Stu_gradeFloat; SelectGradeIntoStu_gradeFromGradewhereStudent_no=Stu_noandCourse_no=Cour_no; IfStu_grade>=90 Then SelectStu_grade,A;ElseIf Stu_grade<90 andStu_grade>=80 Then SelectStu_grade,B;ElseIf Stu_grade<80 andStu_grade>=70 Then select Stu_grade, ' C ' Span class= "pun" >;  ElseIf stu_grade70 and Stu_grade>=< Span class= "lit" >60 then select  Stu_grade, ' D ' ; else select Stu_grade, ' E ' ;end if; end                

Note: If as a statement, you need to add a semicolon after End if ";" The other statements, such as case, loop, are the same as the statement ends.

Summary of MySQL if,case statement usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.