0

I have a simple mysql datatable like this:

users:
user_id score
1          0
2         20
3       1430
4        820
5        170

Im using the following mysql query to get the rank of a user with a certain user_id:

SELECT FIND_IN_SET( score, (
                    SELECT GROUP_CONCAT( score
                        ORDER BY score DESC ) 
                    FROM users )
                ) AS rank
                  FROM users WHERE user_id = 4

and in my mysql it returns me correctly

rank
2

because user_id=4 has the 2nd highest score here.

But how does the corresponding query look like in nodejs? My copy-paste code returns an error:

var query = connection.query('SELECT FIND_IN_SET( score, (
                    SELECT GROUP_CONCAT( score
                        ORDER BY score DESC ) 
                    FROM users )
                ) AS rank
                  FROM users WHERE user_id = 4', function(err, result){
    console.log(err);
});

with the error:

    var query = connection.query('SELECT FIND_IN_SET( score, (
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected token ILLEGAL
MojioMS
  • 1,583
  • 4
  • 17
  • 42
  • thx, but this was just another copy-paste error. I actually dont have "users" in the query. Edited the query in the question. Error is still the same – MojioMS Apr 13 '16 at 12:17

1 Answers1

1

You can't have multiline strings in JavaScript without escaping the line returns or using ES6 template. Try

var query = connection.query("SELECT FIND_IN_SET( score, (\
                SELECT GROUP_CONCAT( score\
                    ORDER BY score DESC ) \
                FROM users )\
            ) AS rank\
              FROM users WHERE user_id = 4", function(err, result){
console.log(err);
});
christophetd
  • 3,834
  • 20
  • 33
  • 1
    multiline queries work for other queries in my nodejs script. I will try it out now tho.. – MojioMS Apr 13 '16 at 12:23
  • oh, it worked. And after having a closer look, I noticed I didnt have any multiline strings in the script yet. So thanks alot! – MojioMS Apr 13 '16 at 12:25
  • You're welcome! Please accept my answer if it solved your issue. You can also take a look at [this topic](https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript), it contains alternatives that may interest you. :) – christophetd Apr 13 '16 at 12:29
  • On SO, you have to wait a couple of minutes after asking a question for accepting an answer – MojioMS Apr 13 '16 at 12:37
  • Oh OK, didn't know that – christophetd Apr 13 '16 at 12:41