Home> Database> SQL> body text

Conditional judgment statements in sql

下次还敢
Release: 2024-04-28 11:36:13
Original
324 people have browsed it

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 statements in sql

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:

  • IF-ELSEstatement: Evaluates a condition and executes one statement if the condition is true, otherwise executes another statement.
  • CASEStatement: Evaluate a condition and execute the corresponding statement based on different condition values.

Syntax

IF-ELSE statement

IF  THEN  ELSE  END IF;
Copy after login

CASE statement

CASE  WHEN  THEN  WHEN  THEN  ... ELSE  END CASE;
Copy after login

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;
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!