在MySQL中,可以使用CASE語句來實現類似于decode函數的功能。以下是一些實用技巧:
SELECT
CASE column_name
WHEN value1 THEN result1
WHEN value2 THEN result2
ELSE default_result
END AS decoded_value
FROM table_name;
SELECT
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END AS decoded_value
FROM table_name;
SELECT
CASE
WHEN column_name BETWEEN value1 AND value2 THEN result1
WHEN column_name BETWEEN value3 AND value4 THEN result2
ELSE default_result
END AS decoded_value
FROM table_name;
SELECT
CASE
WHEN column_name IS NULL THEN 'NULL'
ELSE column_name
END AS decoded_value
FROM table_name;
這些技巧可以幫助您在MySQL中實現類似于decode函數的功能。通過靈活運用CASE語句,可以實現多種條件判斷和值轉換操作。