Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MySQL在一条查询语句中更新多行_Mysql_Node.js - Fatal编程技术网

Node.js MySQL在一条查询语句中更新多行

Node.js MySQL在一条查询语句中更新多行,mysql,node.js,Mysql,Node.js,我有一个名为UserRequests的表。我需要将RequestStatus更新为Completed,Time为current DateTime,以获取一些请求记录。我正在使用node.js mysql模块 通常,对于单个记录更新,我会执行以下操作: connection.query('update UserRequests set RequestStatus = ?, ReqCompletedDateTime = ? where idRequest = ?', ['Completed', ne

我有一个名为UserRequests的表。我需要将RequestStatus更新为Completed,Time为current DateTime,以获取一些请求记录。我正在使用node.js mysql模块

通常,对于单个记录更新,我会执行以下操作:

connection.query('update UserRequests set RequestStatus = ?, ReqCompletedDateTime = ? where idRequest = ?', ['Completed', new Date(), hReqId], function(err, rows){
    connection.release();
});
但是在我的例子中,我需要用completed和current datetime的状态更新多个UserRequests行

我正在列表中获取所有更新的请求ID。问题是我如何编写查询语句,它将同时更新所有这些语句

我尝试使用multiplequery语句,但它不起作用。另外两种解决方案也不起作用。我尝试过的其他解决方案很少:

connection.query('update UserRequests set RequestStatus = ?, ReqCompletedDateTime = ? where idRequest = ?', [['Completed', new Date(), 'somerowid1'], ['Completed', new Date(), 'somerowid2']], function(err, rows){
  connection.release();
});


我做错了什么?

我不知道您可以用任何语言向查询传递单独的参数列表。如果可以,它将生成对数据库的多个查询

您可以通过将所有值放入in列表中来实现这一点。查询如下所示:

update UserRequests
    set RequestStatus = ?,
        ReqCompletedDateTime = now()
    where idRequest in (?, ?);
在守则中:

connection.query('update UserRequests set RequestStatus = ?, ReqCompletedDateTime = now() where idRequest in (?, ?)', ['Completed',  'somerowid1', 'somerowid2'], function(err, rows) {
遗憾的是,您无法将中的值列表参数化。您需要为每个值指定一个单独的占位符,或者需要将列表直接插入不推荐的查询字符串中


还要注意,ReqCompletedDateTime使用database now函数,而不是应用程序中的日期/时间。这确保了列的一致性,并且不受其他机器上时钟的影响。

您正在寻找“IN”操作符,请尝试以下操作:

connection.query('update UserRequests set RequestStatus = ?, ReqCompletedDateTime = ? where idRequest IN (?)', ['Completed', new Date(), idsArray.join()], function(err, rows){
  connection.release();
});
您的“idsArray”应如下所示:

idsArray = [1,2,3,4];
“join”函数将数组转换为字符串

看看这个答案:

请注意,我没有专门针对您的案例测试此代码,但它应该可以工作!
祝你好运

我尝试了此代码,但它仅使用“somerowid1”更新该行,而somerowid2行未更新。我尝试了您的解决方案,虽然它不会给我任何错误,但也不会更新行。您可以发布您得到的响应吗?在回调返回的rows var中,它显示:{fieldCount:0,affectedRows:0,insertId:0,serverStatus:2,warningCount:0,消息:匹配的行数:0更改:0警告:0,协议41:true,changedRows:0}你确定你在“idsArray”中提供的id确实存在吗?如果你没有收到错误,但没有行受到影响,那么可能你的id是错误的..不确定..我正在从用户选择需要更新的记录的页面读取id。id被读取为:var hReqIdsList=req.body.hReqIdsList。然后在查询中我执行hReqIdsList.join
idsArray = [1,2,3,4];