0

i have an sql query as follows

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) ;

works like a magic on terminal, but on node.js it's having sql query error

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,

0

1 Answer 1

1

You have used the placeholder( ? ) twice while passing only once, you need to pass twice( [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);
        }
    });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.