2017-10-20 40 views
0

我有一個SQL查詢,對我來說,看起來不錯,但我得到一個錯誤。錯誤是:在我的SQL查詢語法錯誤:CASE錯誤

ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS region) FROM country as c INNER JOIN country_translation AS t ON c.id = t.cou' at line 1

查詢:

query = "SELECT DISTINCT c.id, c.shortname, c.name, (CASE WHEN rt.name is NULL then '' else rt.name" \ 
       " AS region) " \ 
       "FROM country as c " \ 
       "INNER JOIN country_translation AS t ON c.id = t.country_id AND t.locale = '{locale}' " \ 
       "INNER JOIN region_translation AS rt ON c.region_id = rt.region_id " \ 
       "AND rt.locale = '{locale}' " \ 
       "INNER JOIN outlet AS o ON o.billing_country = c.shortname " \ 
       "INNER JOIN merchant AS m ON o.merchant_id = m.id " \ 
       "INNER JOIN offer_ent_active AS ofr ON o.merchant_id = m.id " \ 
       "WHERE m.category = '{category}' " 

的問題似乎是在第一次INNER JOIN。這裏有什麼問題?

+1

你'CASE'缺少一個'END' –

+0

@GiorgosBetsos我改變了查詢,看起來像'「SELECT DISTINCT c.id,c.shortname,c.name,(CASE WHEN rt.name IS NULL THEN''ELSE rt.name END AS region)「'但我仍然得到相同的錯誤 –

+2

嘗試'... END)AS region',或者完全刪除括號。無論如何,你不需要它們。 –

回答

0

CASE語句的語法是

CASE expression 
     WHEN condition1 THEN result1 
     WHEN condition2 THEN result2 
     ... 
     WHEN conditionN THEN resultN 
     ELSE result 
    END 

在您的查詢的情況下語句丟失END

CASE WHEN rt.name is NULL 
    THEN '' 
    ELSE rt.name 
END AS region