我有一个像这样的简单mysql数据表:
users:
user_id score
1 0
2 20
3 1430
4 820
5 170
我使用以下mysql查询来获取具有特定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
在我的mysql中它正确地返回
rank
2
因为user_id = 4在这里得分第二高。
但是相应的查询在nodejs中是怎么样的? 我的复制粘贴代码返回错误:
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);
});
错误:
var query = connection.query('SELECT FIND_IN_SET( score, (
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected token ILLEGAL
答案 0 :(得分:1)
您不能在JavaScript中使用多行字符串而不必转义行返回或使用ES6 template。尝试
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);
});