我需要使用list中的where子句执行多行删除查询(id1,id2.)但是,由于JS中没有列表,我在将参数传递给query.This时遇到了问题,这是我的代码:
let list =[1,2,..];
let Query = 'DELETE FROM Table WHERE id IN (?)';
connection.query(Query,list, function (err, result) {
`enter code here`
};当我以这种方式传递它时,在将mysql服务器记录到日志文件之后,我会看到它实际上只传递了第一个id。
PS :我试过不带括号吗?同时在列表上做一个Array.join,但两者都不起作用。
发布于 2017-07-06 00:15:42
只需将项列表附加到查询字符串:
let list = [1, 2, 3];
let Query = `DELETE FROM Table WHERE id IN (${list})`;
connection.query(Query, function (err, result) {
`enter code here`
};发布于 2018-12-04 06:57:13
不幸的是,?占位符不能使用IN运算符。所以你应该避开这些值。假设list变量来自外部源;因此,为了防止SQL注入,您可以:
// `list` is filled in outside this process
const Query = `DELETE FROM Table WHERE id IN (${list.map((item) => connection.escape(item))})`;
connection.query(Query, function (err, result) {
// handle error or result here
};我注意到list.map(connection.escape)不能工作,所以抛出:TypeError: Cannot read property 'config' of undefined
https://stackoverflow.com/questions/44937782
复制相似问题