CASE statement in database sql~
CASE statement in SQL statement and switch statement in high-level language, is the syntax of the standard SQL, applies to a conditional judgment of a variety of values, respectively, to perform different operations
Flexible application of the CASE statement can make the SQL statement has become concise and easy to read.
How to use Case:
Case has two formats. Simple Case function
and Case search function. –Simple Case
–Case Search Function
CASEWHENsex=’1′ THEN ‘male’
These are two ways to achieve the same function. Simple Case functions are written in a relatively concise way, but compared to Case search functions, there are some limitations in terms of functionality, such as writing judgmental formulas.
One more thing to note, the Case function only returns the first value that matches the condition, the rest of the Case part will be automatically ignored.
–For example, in the following SQL, you will never get the result “second case”
CASEWHENcol_1IN(‘a’,’b’) THEN ‘first case’
WHENcol_1IN(‘a’) THEN ‘ Second category’
Meaning and usage of case in c
Case in c is used together with switch to form a switch-case statement to judge a choice, case is used to represent a choice structure.
The general form of a switch statement is:
case constant expression 1: statement 1;
case constant expression 2: statement 2;
case constant expression n: statement n;
default: statement n+1;}
What is a case statement?
The case statement is a multi-branch selection statement. case statement is a multi-branch selection statement. if statement has only two branches to choose from, whereas multi-branch selection structure is often needed in real problems.
For example, student achievement classification (90 points or more for A,……); demographic classification (by age into old, middle, youth, young, young) and so on. Of course, these can be realized in the form of nested if statements, but if there are more branches, there are too many layers of nested if statements, and the program is lengthy and reduces readability, as well as bringing great difficulty to modification.
The Pascal language provides a case statement that directly handles multi-branch selection. The case statement consists of a selection expression and a sequence of selectable operations, and the runtime selects one of the many branches based on the result of the expression’s evaluation.
1, case of the English meaning of “case”, “situation” meaning, we may understand the case statement: when the value of the expression matches a value in the table of constants, then the corresponding statement will be executed after it. Execute the corresponding statement after it; if there is no constant in the constant table that matches the expression, then execute the corresponding statement after the else.
2, else can be omitted, if there is no value matching the expression of the constant table, the program will run down and jump out of the case statement.
3. The reserved word end appears in pairs with the reserved word case, and this end indicates the end of the case statement.