我試圖重命名數據庫的一列的值,我有READ-ONLY訪問權限。我也想把兩個值合併成一個。我正在使用SQL CASE查詢,但它正在轉換值不正確。這是我的SQL:SQL Case語句錯誤地替換值
SELECT
DATE(appraisal.created_time),
appraisal_steps.title,
Count(appraisal.appraisal_id),
case appraisal_steps.title
when "archive" then "A_Archived"
when "on_sale" then "J_Selling"
when "under_evaluation" then "F_Evaluating"
when "evaluated" then "H_Appraisal Complete"
when "pending" then "B_Pending"
when "crap" then "C_Not For Lofty"
when "waiting_internal_expert_evaluation" then "D_Unclaimed"
when ("expert_needs_information" OR "admin_needs_information") then "E_Waiting"
when "seller_answered_to_expert_question" then "G_Needs_Attn"
when "ready_to_be_sold" then "I_Ready"
else "wtf"
end as Status
FROM
appraisal
INNER JOIN appraisal_steps ON appraisal.`status` = appraisal_steps.step_id
WHERE
appraisal.powersale = 0
GROUP BY
DATE(appraisal.created_time),
appraisal_steps.title
ORDER BY
appraisal_steps.title DESC, status
結果是對於一些狀態(正確的是複數爲static :)但seller_answered_to_expert_question轉換爲「E_Waiting」,這是不正確的?。
如果我改變「When」子句的順序,不同的職位並不起作用。
我在做什麼錯?
什麼SQL的味道?此外,在英文中,複數是狀態 - 我不知道拉丁文,但我們通常不會在計算器上說出它;) – Blorgbeard
我會說'狀態'的複數是'狀態',但那並不是'不會影響你的SQL。如果將OR子句分爲兩個單獨的WHEN子句會發生什麼?我期待它的工作。 'ready_to_be_sold'映射到什麼地方?是否有任何東西映射到'wtf'?我認爲正在發生的事情是,appraisal_steps.title與'(「E」或「A」)作爲'appraisal_steps.title =(「E」或「A」)'進行比較,並且這是給出TRUE(所以,我預計準備出售映射到'E_waiting'和'appraisal_steps.title'中的任何無效代碼。 –
猜測MySQL,我測試過的所有其他RDBMS甚至無法處理'CASE'語句中的'OR'。 –