my sql case when
The SQL CASE WHEN statement is used to perform conditional operations in SQL. It is used to evaluate a condition and return a value based on that condition. The basic syntax of the SQL CASE WHEN statement is as follows:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN condition3 THEN result3
...
ELSE resultN
END
Here, the CASE statement is followed by a series of WHEN conditions, each of which is evaluated in order. If a condition is true, then the corresponding result is returned. If none of the conditions are true, then the ELSE result is returned (if it is specified).
For example, let's say we have a table called "employees" with columns "id", "name", and "salary". We want to create a new column called "salary_class" that categorizes employees based on their salary. We could use the SQL CASE WHEN statement to do this:
SELECT id, name, salary,
CASE
WHEN salary < 50000 THEN 'Low'
WHEN salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'High'
END AS salary_class
FROM employees;
In this example, we are using the CASE WHEN statement to evaluate the salary of each employee and categorize them as "Low", "Medium", or "High" based on their salary range.
原文地址: https://www.cveoy.top/t/topic/bUBF 著作权归作者所有。请勿转载和采集!