I have a RANK
table like this
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| uid | VARCHAR(50) unsigned | NO | PRI | NULL | |
| steps | int(20) unsigned | NO | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
and I want to sort steps by DESC and get row position of specific row using uid which is primary.
I can sort them by
SELECT uid, steps FROM RANK ORDER BY steps DESC
but I have no clue how I can get a row position of specific row using uid. I saw some threads on stackoverflow and tried it on our DB server but it didn't work out well. Is there any way to do this?
I tried:
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, steps, COUNT(*) as ordercount
FROM RANK
GROUP BY steps
ORDER BY ordercount DESC;
but it doesn't get my any result
with sequelizejs
async function getUserData(uid) {
var query = ""
query += "SELECT rank, total_steps_value FROM ("
query += " SELECT @rank := @rank + 1 AS rank, user_id, total_steps_value"
query += " FROM RANK"
query += " CROSS JOIN (SELECT @rank := 0) AS var"
query += " ORDER BY total_steps_value DESC) AS x"
query += `WHERE user_id = ${mysql.escape(uid)}`
let res = await models.sequelize.query(query)
console.log("RES", res)
}
I get this error
SequelizeDatabaseError: ER_PARSE_ERROR: 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 'user_id = '10208360871964156'' at line 1