【问题标题】:sql query not working on node.js mysql but works on terminalsql查询在node.js mysql上不起作用,但在终端上起作用
【发布时间】:2018-12-19 05:40:28
【问题描述】:

我有一个sql查询如下

select profileName from user where (id) in ( select id FROM( SELECT user_id1 as id from user_connection where user_id2=(select id from user where profileName='deva') UNION ALL SELECT user_id2 as id from user_connection where user_id1=(select id from user where profileName='deva')) t GROUP BY id) ;

在终端上像魔术一样工作,但在 node.js 上却出现 sql 查询错误

exports.findFriends = function(userName,callback){
console.log('searching friends.... '+userName);
var findFriendsQuery = 'select profileName from user where (id) in ( select id FROM ( SELECT user_id1 as id from user_connection where user_id2=(select id from user where profileName= ? ) UNION ALL SELECT user_id2 as id from user_connection where user_id1=(select id from user where profileName=?)) t GROUP BY id)' ;
  db.query(findFriendsQuery,[userName],function(err,rows,fields){
    if(err){
     console.log(err);
     callback(1,-1,-1);
     }
    else{
        callback(0,rows,1);
        }
    });
};

sqlMessage: '您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 \'?)) t GROUP BY id)\' at line 1' 附近使用正确的语法, sqlState: '42000', 索引:0,

【问题讨论】:

    标签: mysql node.js


    【解决方案1】:

    你已经使用了两次占位符(?),而只通过了一次,你需要通过两次([userName, userName])

    exports.findFriends = function(userName,callback){
    console.log('searching friends.... '+userName);
    var findFriendsQuery = 'select profileName from user where (id) in ( select id FROM ( SELECT user_id1 as id from user_connection where user_id2=(select id from user where profileName= ? ) UNION ALL SELECT user_id2 as id from user_connection where user_id1=(select id from user where profileName=?)) t GROUP BY id)' ;
      db.query(findFriendsQuery,[userName, userName],function(err,rows,fields){
        if(err){
         console.log(err);
         callback(1,-1,-1);
         }
        else{
            callback(0,rows,1);
            }
        });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多