我的SQL查询中出现语法错误:CASE错误

时间:2017-10-20 05:34:11

标签: mysql sql

我有一个SQL查询,对我来说,看起来不错但我收到错误。错误是:

  

ProgrammingError:1064(42000):您的SQL语法有错误;   检查与您的MySQL服务器版本对应的手册   正确的语法在“国家/地区”附近使用)FROM国家/地区作为c INNER JOIN   country_translation AS t ON c.id = t.cou'在第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 个答案:

答案 0 :(得分: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