Home > Database > SQL > body text

When usage in sql

下次还敢
Release: 2024-05-01 22:39:48
Original
415 people have browsed it

The WHEN clause in SQL is used to specify conditions in a CASE expression and return the corresponding output. The syntax is as follows: CASE WHEN condition THEN result END. When the condition is TRUE, the corresponding result is returned.

When usage in sql

WHEN usage in SQL

What is the WHEN clause?

The WHEN clause is used to specify conditions in a CASE expression and return the appropriate output.

Grammar:

<code>CASE
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ...
  ELSE result_default  -- 可选
END</code>
Copy after login

How to use the WHEN clause?

  1. Specify conditions: condition1, condition2, etc. represent the conditions to be evaluated. When the result is TRUE, the corresponding output will be performed.
  2. Return output: result1, result2, etc. are the values ​​returned after the conditions are met.
  3. Default output (optional): The ELSE clause specifies the default output value when all conditions are not met. If ELSE is not specified, NULL is returned.

Example:

<code>-- 根据成绩计算等级
CASE
  WHEN grade >= 90 THEN '优等'
  WHEN grade >= 80 THEN '良好'
  WHEN grade >= 70 THEN '中等'
  ELSE '不及格'
END</code>
Copy after login

Note:

  • WHEN clauses are evaluated sequentially. Once a certain condition is met, evaluation stops and the appropriate output is returned.
  • CASE expressions can be nested.
  • The WHEN clause can be used in query, update, and insert statements.

The above is the detailed content of When usage 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
Popular Tutorials
More>
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!