mysql - sql查询不适用于node.js mysql,但适用于终端

标签 mysql node.js

我有一个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: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'?)) t GROUP BY id)\' at line 1', sqlState: '42000', index: 0,

最佳答案

您已经使用了两次占位符( ? ),而只传递了一次,您需要传递两次( [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);
        }
    });
};

关于mysql - sql查询不适用于node.js mysql,但适用于终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51281102/

相关文章:

node.js - NPM : checkPermissions Missing write access to/. ../node_modules/is

node.js - nodejs 在 nvm $HOME/nvm/nvm.sh 启动时处于事件状态

MySQL GROUP_CONCAT 重复条目

mysql - Like 查询如何与索引表一起使用

mysql - 如何使用 Laravel 和 MySQL 获取大量记录?

node.js - 如何从nodejs和node-firebird执行多个程序?

javascript - Node.js - 连接到同一个服务器,同一个用户,使用多个 html 文件

mysql - 为什么我导入的数据在 mysql 中变得困惑?

php - 如何将localhost phpmyadmin连接到android模拟器

node.js - 为什么 IndexedDB 在 node.js 中不可用?