i have a table like this
userEmail | views
A 8
B 3
C 4
A 2
B 5
i want to get rank of a particular user say B ..
what i am doing is :
SET @rank=0;
select @rank:=@rank+1 AS rank , userEmail,sum(views) as views from mtable where userEmail != 'null' Group By userEmail Order By views DESC
this will output something like this :
rank | userEmail | views
1 A 10
2 B 8
3 C 4
I can get the rank of all users but what if i want to see rank of only one user ??
if i put where userEmail = 'B' in above query
i will get this :
rank | userEmail | views
1 B 8
but it should be like this :
rank | userEmail | views
2 B 8
As Gordan pointed out my first answer is not very standard. Here is a standard way of doing the same thing.
There is a special way to do this in sql server 2005+ and Oracle (using the
OVERclause) but the general answer (which you have to use with mysql) is to use a where clause. Like this: