There are two common conditional judgment statements in SQL: IF-ELSE statement and CASE statement. The IF-ELSE statement executes different statements depending on whether the condition is true or false, while the CASE statement executes the corresponding statement depending on whether the expression value matches different conditions.
Conditional judgment statement in SQL
Conditional judgment statement is evaluated in SQL Statements that condition and take different actions based on the results. They are widely used in SQL queries and operations to filter data from the database, modify data, or control processes.
Type
There are two commonly used conditional judgment statements in SQL:
Syntax
IF-ELSE statement
<code class="sql">IF <condition> THEN <statement_if_true> ELSE <statement_if_false> END IF;</code>
CASE statement
<code class="sql">CASE <expression> WHEN <case_value1> THEN <statement1> WHEN <case_value2> THEN <statement2> ... ELSE <default_statement> END CASE;</code>
Example
Use the IF-ELSE statement to check if the value is greater than 5
<code class="sql">SELECT * FROM table_name WHERE value IF value > 5 THEN 'Greater than 5' ELSE 'Less than or equal to 5' END IF;</code>
Use the CASE statement to check the score range
<code class="sql">CASE score WHEN 90 TO 100 THEN '优秀' WHEN 80 TO 89 THEN '良好' WHEN 70 TO 79 THEN '中等' ELSE '不及格' END CASE;</code>
The above is the detailed content of Conditional judgment statements in sql. For more information, please follow other related articles on the PHP Chinese website!