MYSQL 和 Node.js 使用 where 子句插入多条记录
MYSQL and Node.js multiple record insert with where clause
我正在尝试使用 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);
我正在尝试使用 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);