我正在尝试使用WHERE子句将多个记录从Node.js插入MYSQL,但是我一直收到语法错误。
该语句可以正常工作,直到我尝试向其添加条件语句为止。然后,我得到此错误:ER_PARSE_ERROR:您的SQL语法中的VALUES附近有错误?在哪里...
var Data = data; // this is a nested array already as received from client side like [[..],[..],[..]]
var ID = 123;
var sql = "INSERT INTO table1 (Col1,Col2,Col3,Col4,Col5) VALUES ? WHERE"+ID+" NOT IN (SELECT somecol FROM table2 WHERE somecol= "+ID+")"
connection.query(sql, [Data], function (error, result) {
if (error) {
throw error;
res.json({ Message: "Oops something went wrong :("});
}
res.json({ Message: "Your data was added!"});
});
该连接已设置为允许多个语句:
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1234',
database: 'thedb',
port: 12345,
charset: "utf8mb4",
multipleStatements: true
});
查询以这种形式工作,而没有WHERE子句:
var Data = data; // this is a nested array already as received from client side like [[..],[..],[..]]
var ID = 123;
var sql = "INSERT INTO table1 (Col1,Col2,Col3,Col4,Col5) VALUES ?"
connection.query(sql, [Data], function (error, result) {
if (error) {
throw error;
res.json({ Message: "Oops something went wrong :("});
}
res.json({ Message: "Your data was added!"});
});
如何使查询与WHERE子句一起使用?
最佳答案
Insert
命令不能与Where
子句一起使用,因为您要插入新行。用天真的话说,Where
子句需要一些行以根据条件过滤掉。根据您的用例,您可以有两种可能的解决方案:
使用Update
语句可能像Update table set col1=val1 where (condition clause)
如果您确实要使用Where
子句,则可以使用以下形式的Insert
命令Insert into table(col1,col2)Select (val1, val2) from table2 where (condition clause);
关于mysql - MYSQL和Node.js具有where子句的多条记录插入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53467855/