I have a 1 table database with (in a simplified form) the following fields: user_id(INT), ref_id(INT), points(INT), pointsgiven(BOOL/TINY_INT)
I want a query that returns the RANK of the user_id I specify based on points, given that pointsgiven is true. The kicker is, I need ties included. I can get a result set for ALL ranks if I want with the following query
SELECT
user_id, ref_id, points, pointsgiven,
CASE
WHEN @points = COALESCE(points, 0) THEN @rownum
ELSE @rownum := @rownum + 1
END AS rank,
@points := COALESCE(points, 0)
FROM users CT
JOIN
(
SELECT @rownum := 0, @points := NULL
) r
WHERE pointsgiven=TRUE ORDER BY points DESC
So, based on that, I thought I could just use that as a subquery to get a certain user_id as follows:
select * from
(
SELECT
user_id, ref_id, points, pointsgiven,
CASE
WHEN @points = COALESCE(points, 0) THEN @rownum
ELSE @rownum := @rownum + 1
END AS rank,
@points := COALESCE(points, 0)
FROM users CT
JOIN
(
SELECT @rownum := 0, @points := NULL
) r
WHERE pointsgiven=TRUE ORDER BY points DESC
) as derived WHERE user_id = 15
But this returns [BLOB - 1 B] as the rank on the correct user_id. What am I doing wrong here?