2017-12-07 14 views
1

こんにちは私の名前は本部MilindとCASEでMySQL UPDATEクエリ火災にしようとWHEN文であり、私は下に、このエラーを取得:キーワードが認識されません。 PHPで

24 errors were found during analysis. 

Unrecognized keyword. (near "CASE" at position 27) 
Unrecognized keyword. (near "WHEN" at position 32) 
Unexpected token. (near "id" at position 37) 
Unexpected token. (near "=" at position 39) 
Unexpected token. (near "'2'" at position 40) 
Unrecognized keyword. (near "THEN" at position 44) 
Unexpected token. (near "'Y'" at position 49) 
Unrecognized keyword. (near "ELSE" at position 53) 
Unrecognized keyword. (near "status" at position 58) 
Unrecognized keyword. (near "END" at position 65) 
Unexpected token. (near "," at position 68) 
Unrecognized keyword. (near "status" at position 71) 
Unexpected token. (near "!=" at position 78) 
Unrecognized keyword. (near "CASE" at position 81) 
Unrecognized keyword. (near "WHEN" at position 86) 
Unrecognized keyword. (near "status" at position 91) 
Unexpected token. (near "=" at position 98) 
Unexpected token. (near "'2'" at position 99) 
Unrecognized keyword. (near "THEN" at position 103) 
Unexpected token. (near "'N'" at position 108) 
Unrecognized keyword. (near "ELSE" at position 112) 
Unrecognized keyword. (near "status" at position 117) 
Unrecognized keyword. (near "END" at position 124) 
Unexpected token. (near "," at position 127) 

と私のUPDETEクエリは以下の通りです:

UPDATE quotes SET status = CASE WHEN id='2' THEN 'Y' ELSE status END, 
status = CASE WHEN id !='2' THEN 'N' ELSE status END, 
WHERE id='2'; 

I status = Y insted of status = Nとstutus = N insted of status = Yのどこで更新したいか。

+1

'!='はSQL構文ではありません。文字列をコンパイルするときには、たとえば次のようになります。 '' id 'like like' 2 'then ' – PhillipD

+1

@PhillipD、'!= 'はANSI SQLの標準構文ではありませんが、ほぼすべてのSQL製品で認識されます。 https://stackoverflow.com/questions/723195/should-i-use-or-for-not-equal-in-t-sql/723426#723426 –

+0

への私の答えを参照してください。@BillKarwin情報とリンクされていただきありがとうございます回答。 – PhillipD

答えて

2

CASE文の使い方を誤解しているようです。以下の例を参照してください。this MySQL tutorial

DELIMITER $$ 

CREATE PROCEDURE GetCustomerShipping(
in p_customerNumber int(11), 
out p_shiping  varchar(50)) 
BEGIN 
    DECLARE customerCountry varchar(50); 

    SELECT country INTO customerCountry 
FROM customers 
WHERE customerNumber = p_customerNumber; 

    CASE customerCountry 
WHEN 'USA' THEN 
    SET p_shiping = '2-day Shipping'; 
WHEN 'Canada' THEN 
    SET p_shiping = '3-day Shipping'; 
ELSE 
    SET p_shiping = '5-day Shipping'; 
END CASE; 

END$$ 
関連する問題