Node.js mysql 查询语法问题 UPDATE WHERE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14992879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:38:37  来源:igfitidea点击:

Node.js mysql query syntax issues UPDATE WHERE

mysqlnode.js

提问by agdurrette

I trying to update some info in MYSQL DB, but I'm not sure of how to do it with in node.js. This is the mysql driver I'm using https://github.com/felixge/node-mysql

我试图更新 MYSQL DB 中的一些信息,但我不确定如何在 node.js 中执行此操作。这是我使用的 mysql 驱动程序https://github.com/felixge/node-mysql

What I have so far

到目前为止我所拥有的

connection.query('SELECT * FROM users WHERE UserID = ?', [userId], function(err, results) {
if (results[0]) {
if (results[0].Name!=name) {
console.log(results[0].Name);
connection.query('UPDATE users SET ? WHERE UserID = ?', [userId], {Name: name});
}
console.log(results[0].UserID);
}
});

Everything works except...

一切正常,除了...

connection.query('UPDATE users SET ? WHERE UserID = ?', [userId], {Name: name});

In PHP I would have this...

在PHP中,我会有这个......

mysql_query("UPDATE users SET Name='".$_GET["name"]."' WHERE UserID='".$row['UserID']."'");

I'm not sure what I'm doing wrong, But I'm positive that the issue is here

我不确定我做错了什么,但我很确定问题就在这里

connection.query('UPDATE users SET ? WHERE UserID = ?', [userId], {Name: name});

回答by ruakh

[Note (added 2016-04-29):This answer was accepted, but it turns out that there isa reasonable way to use SET ?. For details, see Bala Clark's answer on this same page. —ruakh]

[注(加2016年4月29日):这个答案已被接受,但事实证明,还有就是用合理的方式SET ?。有关详细信息,请参阅同一页面上 Bala Clark 的回答。——鲁阿克]



From the code for Connection.prototype.query()and Connection.createQuery(), it's clear that you can only pass in a singlevalues object. I don't see where the code for the special SET ?behavior is defined — it's clearly not in SqlString.formatQuery()— but if it uses SqlString.objectToValues(), then I guess there's no way to use it together with another ?.

的代码Connection.prototype.query()Connection.createQuery(),很明显,你只能通过在一个单一的值对象。我没有看到特殊SET ?行为的代码在哪里定义 - 它显然不在SqlString.formatQuery()- 但如果它使用SqlString.objectToValues(),那么我想没有办法将它与另一个?.

I think the best approach is to just dispense with the neat SET ?feature, and write either of these:

我认为最好的方法是放弃简洁的SET ?功能,并编写以下任一项:

connection.query('UPDATE users SET Name = ? WHERE UserID = ?', [name, userId])
connection.query('UPDATE users SET Name = :Name WHERE UserID = :UserID',
                     {UserID: userId, Name: name})

but if you really want to use SET ?, I suppose you could write this:

但如果你真的想使用SET ?,我想你可以这样写:

connection.query('UPDATE users SET ? WHERE UserID = :UserID',
                     {UserID: userId, Name: name})

which would update both UserIDand Name; unless you have a trigger, this shouldbe O.K., in that it's updating UserIDto the value it already had anyway. But it's kind of disconcerting, and I don't recommend it.

这将更新UserIDName; 除非你有一个触发器,否则这应该没问题,因为它UserID无论如何都会更新到它已经拥有的值。但这有点令人不安,我不建议这样做。

回答by Bala Clark

The answer by ruakhisn't entirely correct, you can use the SET ?feature with another ?.

的答案ruakh并不完全正确,您可以将该SET ?功能与另一个?.

The syntax is:

语法是:

connection.query('UPDATE users SET ? WHERE UserID = ?', [{ Name: name }, userId])

回答by Marcel

You can use an array of objects:

您可以使用一组对象:

connection.query('UPDATE user SET ? WHERE ?', [{ Name: name }, { UserId: userId }])

回答by Shubh

This is what worked for me

这对我有用

 var records = 
 [name,email,adhar,ROLE,NOTES,REQUESTFORSUPPLIER,PASSWORD,LOCATION,CREATEDBY,CREATEDON,MODIFIEDBY,MODIFIEDON,mobile];

    var sql="UPDATE flavica_user SET Name=?,EMAIL=?,AADHAR=?,ROLE=?,NOTES=?,REQUESTFORSUPPLIER=?,PASSWORD=?,LOCATION=?,CREATEDBY=?,CREATEDON=?,MODIFIEDBY=?,MODIFIEDON=? WHERE USER_MOBILE=?"
    //var sql="UPDATE `flavica_user` SET ? WHERE ?";
    //this sql updates flavica_user
    con.query(sql,records,(err,result)=>{

      if(err){
        res.json({
          status:400,
          message:err
        })
      }

      else{
        res.json({
          status:200,
          message:result
        })
      }

    })