2016-12-22 159 views
1

蔭嘗試寫爲下面的表結構的CASE語句:如果Case語句的MySql

CASE WHEN (tender_cust_shortcode = 'HAL Hydera' OR 'HAL ARDC') THEN 'N/A' 
    WHEN (tender_cust_shortcode != 'HAL Hydera' OR 'HAL ARDC') AND open_auth_date IS NULL 
THEN 'Pending' 
ELSE 'Completed' END AS `Open Auth Status` 

的標準是

1):

======================================== 
tender_cust_shortcode | open_auth_date 
======================================== 
HAL Hydera   | 24-01-2016 
DRDL     | NULL 
HAL ARDC    | 10-02-2016 
DLRL     | NULL 
ISAC     | NULL 
======================================== 

我的CASE語句如下(tender_cust_shortcode ='HAL Hydera'或'HAL ARDC')那麼它應該是'不適用' hortcode!='HAL Hydera'或'HAL ARDC')並且open_auth_date IS NULL,那麼它應該導致爲'Pending'

3)Else'Completed'。

Iam沒有得到所需的輸出。可能是AND或OR條件問題。請任何人都可以幫助我呢?

回答

1

嘗試使用IN,不知道這是否有效,但嘗試它:

CASE 
    WHEN tender_cust_shortcode IN ('HAL Hydera', 'HAL ARDC') THEN 'N/A' 
    WHEN tender_cust_shortcode NOT IN ('HAL Hydera', 'HAL ARDC') AND open_auth_date IS NULL THEN 'Pending' 
    ELSE 'Completed' 
END AS `Open Auth Status` 

這裏是SQLFiddle Demo,它的工作原理。:-)

+0

奇妙。它的工作正常。非常感謝。 –

0

你必須repead列名,或不平等的條件必須由AND連接:

CASE WHEN (tender_cust_shortcode = 'HAL Hydera' OR tender_cust_shortcode = 'HAL ARDC') THEN 'N/A' 
    WHEN (tender_cust_shortcode != 'HAL Hydera' AND tender_cust_shortcode != 'HAL ARDC') AND open_auth_date IS NULL 
THEN 'Pending' 
ELSE 'Completed' END AS `Open Auth Status` 

SQLFiddle

+0

'待定' 它沒有顯示。在這種情況下,'ISAC'是待定。但根據上面顯示的「已完成」的情況說明。我犯了什麼錯誤嗎? –

+0

@SanjuMenon在你的情況下''open_auth_date'是否爲空? – Jens

+0

不需要括號。 – axiac

-1

嘗試這樣的查詢:

SELECT CASE WHEN (tender_cust_shortcode = 'HAL Hydera' OR 'HAL ARDC') THEN 'N/A' 
WHEN ((tender_cust_shortcode != 'HAL Hydera' OR 'HAL ARDC') AND (open_auth_date IS NULL)) THEN 'Pending' 
ELSE 'Completed' END FROM `test` 
+0

比較運算符('=')具有[更高的優先級](http://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html) '。這意味着'tender_cust_shortcode ='HAL Hydera'或'HAL ARDC'被評估爲'(tender_cust_shortcode ='HAL Hydera')或'HAL ARDC',這不是OP所需要的。 – axiac

+0

@Avani。感謝您的時間。 –