Use summary of IF and case statements in Mysql _mysql

Source: Internet
Author: User
Tags numeric value

The if of MySQL can be used as an expression or in a stored procedure as a Process Control statement, as in the following is used as an expression:

An If expression

Copy Code code as follows:

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 the IF () is a numeric value or a string value, depending on the context in which it is located.
Copy Code code as follows:

Select *,if (sva=1, "male", "female") as Ssva from Taname where SVA!= ""

an If of an expression can also be implemented with case:
Copy Code code as follows:

Select Case SVA when 1 THEN ' ELSE ' woman ' end as Ssva from Taname where SVA!= '

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

For example:

Copy Code code as follows:

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, then 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.

Copy Code code as follows:

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 value for Ifnull (EXPR1,EXPR2) is one of the more "generic" in two expressions, in the order of string, real, or INTEGER.

IF ELSE is used as a Process Control statement

If the implementation of conditions to determine, to meet different conditions to perform different operations, which we learn to program all know if the role of the following, we look at the MySQL stored procedures in how to use the.

Copy Code code as follows:

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 is set up, the then Statement_list statement is executed, otherwise the condition in ElseIf is judged, and then the Statement_list statement is executed. Otherwise continue to judge the other branches. Executes the Else branch when none of the conditions for all branches are true. Search_condition is a conditional expression that can be composed of conditional operators such as "=, <, <=, >, >=,!=," and can use and, or, not to combine multiple expressions.

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

Copy Code code as follows:

CREATE PROCEDURE Dbname.proc_getgrade
(stu_no varchar, 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 as a statement, you need to add a semicolon after End if ";" To indicate the end of the statement, other statements such as Case, loop, and so on are the same.

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.