我有一个 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', index: 0 附近使用正确的语法
您使用了 placeholder( ?
) 两次,而只传递一次,您需要传递两次 ( [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);
}
});
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句