2017-10-20 109 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