在SELECT语句中使用CASE表达式可以根据特定的条件来返回不同的值。以下是使用CASE表达式的一些示例:
SELECT
column1,
column2,
CASE
WHEN condition1 THEN value1
WHEN condition2 THEN value2
ELSE value3
END AS new_column
FROM table_name;
SELECT
column1,
column2
FROM table_name
WHERE
CASE
WHEN column1 = 'value1' THEN 'condition1'
WHEN column2 = 'value2' THEN 'condition2'
ELSE 'condition3'
END = 'condition1';
SELECT
column1,
SUM(CASE
WHEN column2 = 'value1' THEN 1
ELSE 0
END) AS count_value1,
SUM(CASE
WHEN column2 = 'value2' THEN 1
ELSE 0
END) AS count_value2
FROM table_name
GROUP BY column1;
通过这些示例,你可以更好地了解如何在SELECT语句中使用CASE表达式来根据不同条件返回不同的结果。