JOIN语句中的SQL语法错误

时间:2013-05-07 14:02:53

标签: mysql pdo

$stmt = $dbo->getConnection()->prepare("SELECT count(character_name)
            FROM playercharacter
            JOIN character
            ON (playercharacter.character_id = character.character_id)
            WHERE user_id = :user_id");

给我

  

致命错误:带有消息的未捕获异常'PDOException'   'SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在'字符ON附近使用正确的语法   (playercharacter.character_id = character.character_id)'在第3行'   /home/content/50/10835750/html/dogdays/game/private.php:43堆栈   追踪:#0 /home/content/50/10835750/html/dogdays/game/private.php(43):   PDOStatement->执行(数组)#1 {main}抛出   第43行/home/content/50/10835750/html/dogdays/game/private.php

1 个答案:

答案 0 :(得分:3)

character是保留关键字,恰好是您的表格的名称。为了避免语法错误,应使用反引号转义表名。前,

SELECT  count(character_name) 
FROM    playercharacter JOIN `character` 
            ON (playercharacter.character_id = `character`.character_id) 
WHERE   user_id = :user_id

如果您有权更改表格,请更改保留关键字列表上不存在的表名称,以防止同一错误再次返回。