我正在尝试对MySQL数据库运行多语句查询。我正在使用node-mysql和ExpressJS。我通过将multipleStatement设置为true启用了多个语句查询:
const connection = mysql.createPool(config.databaseOptions, {multipleStatements: true});
我有以下多条语句查询:
app.get('/information', urlencodedParser, function (req, res) {
var mysql = require('./resources/mysqlConnection');
var qry = "SELECT e.emp_id, company_id, age, country FROM employee as e INNER JOIN company as c ON e.emp_id = c.emp_id WHERE e.emp_id = ?;SELECT * from players as p INNER JOIN teams as t ON p.team_id = t.team_id WHERE country = ?";
mysql.query(qry, ["E00909", "Japan"], function(err, rows) {
if (err) throw err;
console.log(rows);
var emp_data = rows[0];
var team_data = rows[1];
res.render('info.ejs', {data : emp_data});
});
});
但是,当我运行服务器时,出现以下错误:
Error: ER_PARSE_ERROR: 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 'SELECT * from players as p INNE'
我已经阅读了其他有关多语句查询的文章,但是所有这些情况的解决方案都是通过将multipleStatement设置为true来启用多语句查询。我已经这样做了,所以我不确定为什么会收到这个错误。我认为代码中的
["E00909", "Japan"]
数组是此问题的根源,但我不确定如何解决它。进一步将数组写为['E00909', 'Japan']
并不能解决问题。任何见解,不胜感激。 最佳答案
我会尝试在您的config.databaseOptions对象中设置选项,例如
const config = {
databaseOptions: {
host: "some_host",
user: "some_user",
password: "some_pw",
database: "some_db",
multipleStatements: true
}
}
或者只是尝试:
config.databaseOptions.multipleStatements = true;
在创建池之前。
这为我解决了错误!
如果我省略此行,我将得到与您相同的错误:
ER_PARSE_ERROR: 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 'SELECT..