case when and or
时间: 2023-10-12 16:09:22 浏览: 43
CASE WHEN and OR are two different logical operators in SQL.
CASE WHEN is used to evaluate a condition and return a value based on that condition. It is typically used in a SELECT statement to create a new column with custom values based on certain conditions. The syntax is as follows:
```
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result3
END
```
For example, if we want to create a new column called "age_group" based on the age of a person, we could use the following code:
```
SELECT name, age,
CASE
WHEN age < 18 THEN 'Under 18'
WHEN age >= 18 AND age < 30 THEN '18-29'
WHEN age >= 30 AND age < 50 THEN '30-49'
ELSE '50 and over'
END AS age_group
FROM people;
```
OR is used to combine multiple conditions in a WHERE or HAVING clause. It returns true if any of the conditions are true. The syntax is as follows:
```
SELECT *
FROM table
WHERE condition1 OR condition2;
```
For example, if we want to select all the records from a table where the age is either less than 18 or greater than 65, we could use the following code:
```
SELECT *
FROM people
WHERE age < 18 OR age > 65;
```
相关推荐
![epub](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)