我有一个如下的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服务器版本相对应的手册,以在第1行的\'?))t GROUP BY id)\'附近使用正确的语法, sqlState:“ 42000”, 索引:0,
答案 0 :(得分: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);
}
});
};