An If statement for SQL

Source: Internet
Author: User
Tags numeric value

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=1as fromWHERE!= ";
The IF as an expression can also be implemented with case:
SELECT  Case SVA  when 1  Then ' male ' ELSE ' female '   
END as
from table_name WHERE != ‘‘;

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:

SELECT  Case 1  when 1  Then '  One '  when 2  Then '  Both '    ELSE '  More '
as Test

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.

 SELECTIfnull (1,0);  - 1SELECTIfnull (NULL,Ten);  - TenSELECTIfnull (1/0,Ten); - TenSELECTIfnull (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
Then statement_list [ELSEIF search_conditionthen] statement_list ... [ELSE statement_list] END IF

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 PROCEDUREDbname.proc_getgrade (Stu_novarchar( -), Cour_novarchar(Ten))  BEGIN DECLAREStu_gradefloat; SELECTGrade intoStu_grade fromGradeWHEREStudent_no=Stu_no andCourse_no=Cour_no; IFStu_grade>= -  Then     SELECTStu_grade,'A'; ELSEIF Stu_grade< -  andStu_grade>= the  Then     SELECTStu_grade,'B'; ELSEIF Stu_grade< the  andStu_grade>= -  Then     SELECTStu_grade,'C'; ELSEIF Stu_grade70 andStu_grade>= -  Then      SELECTStu_grade,'D'; ELSE     SELECTStu_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.

An If statement for SQL

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.