I need some help with a MySQL query. I'm trying to rank participants using a WHERE clause. So, I'd like to rank novices, intermediates, and experienced separately. For example:
Rank Name Grade Type
----------------------------------
1 Bob 98 Novice
2 Jill 88 Novice
3 Jimmy 42 Novice
1 Mark 87 Intermediate
2 Scott 85 Intermediate
3 Jim 77 Intermediate
1 Jane 90 Advanced
2 John 89 Advanced
3 Josh 87 Advanced
I've tried:
SET @rank=0;
(SELECT @rank:=@rank+1 AS rank, name, grade, type FROM myTable WHERE type='novice' ORDER BY grade DESC)
UNION ALL
(SELECT @rank:=@rank+1 AS rank, name, grade, type FROM myTable WHERE type='intermediate' ORDER BY grade DESC)
UNION ALL
(SELECT @rank:=@rank+1 AS rank, name, grade, type FROM myTable WHERE type='experienced' ORDER BY grade DESC)
I guess that I need to re-set the rank somehow. Maybe I have another problem?