2010-06-28 75 views
2

試圖做到這一點:是否可以在WHERE中使用CASE?

SELECT CASE WHEN field = true THEN one * another ELSE one END as case_field 
FROM table WHERE case_field >= 9000 

和接收case_field不存在的錯誤。

有沒有可能做到這一點,而無需重複CASE?

回答

7

看起來像PostgreSQL的支持derived tables

SELECT * FROM 
(
SELECT CASE WHEN field = true THEN one * another ELSE one END as case_field 
FROM table 
) AS derived 
WHERE case_field >= 9000 

您可能要檢查它不會對執行計劃的任何負面影響。

+0

@Milen - 乾杯更新的鏈接! – 2010-06-28 11:56:36

0

使用臨時表:

SELECT * 
FROM (
    SELECT 
     CASE WHEN field = true THEN one * another 
     ELSE one 
     END as case_field 
     FROM table 
    ) a 
WHERE a.case_field >= 9000 
+4

這不是一個臨時表,它是一個子查詢。 – 2010-06-28 11:52:38

相關問題