我试图用where子句将node.js中的多条记录插入mysql,但一直出现语法错误。
在我尝试向语句中添加条件语句之前,该语句可以正常工作。然后我得到了这个错误:er\u parse\u error:您的sql语法在值附近有错误吗?哪里。。。
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子句进行查询?
1条答案
按热度按时间hzbexzde1#
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);