2013-05-07 81 views
0

如何檢查在SQL中使用if語句多個條件使用使用多個條件,如果在SQL語句中

在僞

If survey_field is 1 and emp_code_field = 1 and post_field = 1 THEN set chance to 10 
else if survey_field is 1 and emp_code_field = 1 THEN set chance to 5 
survey_field is 1 THEN set chance to 1 

我曾嘗試以下,但我不能夠使用多個條件。

Update employee 
SET chance = 
CASE survey 
WHEN 1 THEN 1 

END 

WHERE 1 
+1

哪個數據庫是這樣嗎? mysql直接支持'if()'語句,而且大多數正常的sql數據庫都會'case'。 – 2013-05-07 16:45:46

+0

刪除PHP,因爲它與此問題無關。 OP,添加你使用的數據庫作爲標籤 - 我假設它是MySQL – raidenace 2013-05-07 16:46:39

回答

1

你可以嘗試這樣的..

UPDATE employee 
    SET chance = Case when 
    (survey_field=1) AND (emp_code_field = 1) AND (post_field = 1) THEN 10 
    ELSE 
    WHEN (survey_field =1) AND (emp_code_field = 1) THEN 5 
    ELSE 
    WHEN 
    survey_field = 1 THEN 1 
    END 
相關問題