尝试使用连接时,我的SQL语法出错

时间:2016-02-23 22:02:52

标签: mysql mariadb

我目前正在尝试执行此sql查询,但我不是SQL中最棒的:

var sqlQuery = `
    SELECT user.ID, user.email, user.first_name, user.last_name,
    user.address1, user.address2, user.country, user.gender  
    FROM subscriber AS user 
    LEFT JOIN (
        SELECT sID, name, experience, locked  
        FROM chars
    ) AS char 
      ON user.ID = char.sID 
    WHERE user.ID = ?
`;

但是我收到了这个错误:

[1] Errors  { [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'char
[1]           ON user.ID = char.sID
[1]         WHERE user.ID = 13' at line 4]
[1]   code: 'ER_PARSE_ERROR',
[1]   errno: 1064,
[1]   sqlState: '42000',
[1]   index: 0 }

任何信息都会非常感谢。

2 个答案:

答案 0 :(得分:3)

Char是一个保留关键字,可以在https://dev.mysql.com/doc/refman/5.7/en/keywords.html查找。

所以将代码改为此,就可以了。通常远离与数据类型冲突的变量名称。

( SELECT sID, name, experience, locked  
FROM chars ) AS c

答案 1 :(得分:0)

语法是LEFT JOIN chars as char

var sqlQuery = `
SELECT user.ID, user.email, user.first_name, user.last_name,
user.address1, user.address2, user.country, user.gender  
FROM subscriber AS user 
LEFT JOIN chars AS char 
  ON user.ID = char.sID 
WHERE user.ID = ?

为什么你还想要名字,经验和锁定的字段呢?