SQL语句中的错误(#1064 - 您的SQL语法中有错误)

时间:2012-10-01 11:19:35

标签: mysql sql mysql-error-1064

SELECT add.id_address, 
       add.alias, 
       add.firstname, 
       add.lastname, 
       add.postcode, 
       add.city, 
       add.address1, 
       add.address2, 
       cl.name as country
FROM address add, 
     country_lang cl
WHERE add.id_customer=2 AND cl.id_lang=3 
AND add.id_country=cl.id_country AND add.active=1 
ORDER BY add.id_address ASC

3 个答案:

答案 0 :(得分:5)

请勿使用add作为表别名。这是一个保留的关键字。

答案 1 :(得分:1)

addreserved keyword您必须将其转义或使用其他别名,例如FROM address a而不是FROM address add,如下所示:

SELECT a.id_address, a.alias, a.firstname,
  a.lastname, a.postcode, a.city, 
  a.address1, a.address2, cl.name as country
FROM address a, country_lang cl
WHERE a.id_customer = 2 AND cl.id_lang=3 
  AND a.id_country = cl.id_country 
  AND a.active=1 
ORDER BY a.id_address ASC

答案 2 :(得分:1)

如果您想以下列方式使用backtick,则必须使用reserved keyword

  SELECT `add`.id_address, `add`.alias, `add`.firstname,
      `add`.lastname, `add`.postcode, `add`.city, 
      `add`.address1, `add`.address2, cl.name as country
    FROM address `add`, country_lang cl
    WHERE `add`.id_customer = 2 AND cl.id_lang=3 
      AND `add`.id_country = cl.id_country 
      AND `add`.active=1 
    ORDER BY `add`.id_address ASC

除了例外,我建议您按以下方式使用JOIN:

SELECT a.id_address, 
       a.alias, 
       a.firstname, 
       a.lastname, 
       a.postcode, 
       a.city, 
       a.address1, 
       a.address2, 
       cl.name as country
FROM address a JOIN country_lang cl ON a.id_country=cl.id_country
WHERE a.id_customer=2 AND cl.id_lang=3 AND a.active=1 
ORDER BY a.id_address ASC