在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表達式來根據不同條件返回不同的結果。