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 statementis 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
IF THEN ELSE END IF;
CASE statement
CASE WHEN THEN WHEN THEN ... ELSE END CASE;
Example
Use the IF-ELSE statement to check if the value is greater than 5
SELECT * FROM table_name WHERE value IF value > 5 THEN 'Greater than 5' ELSE 'Less than or equal to 5' END IF;
Use the CASE statement to check the score range
CASE score WHEN 90 TO 100 THEN '优秀' WHEN 80 TO 89 THEN '良好' WHEN 70 TO 79 THEN '中等' ELSE '不及格' END CASE;
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!