I’ve the following query.
SET @rownum := 0;
SELECT result.rank, result.postid FROM (
SELECT CASE @rownum
WHEN 5 THEN @rownum:=1 ELSE @rownum:=@rownum + 1 END AS rank
,c.postid
FROM comments c
ORDER BY c.postid DESC
) as result
It returns me the result in the following form.
rank postid
---------------------------------
1 199
2 199
3 199
4 199
5 198
1 198
2 198
3 198
4 198
5 198
6 198
Now I wanted to update this query such that ranks are specified according to postid.postid 199 will be ranked from 1 to 4 and as soon as it identifies that a new postid has appeared it should again rank it from 1 to whatever number of records that id contains.
I want the count of rank for each postid to start from 1 and goto the number of times that postid is appearing.
Edit:
@lieven it’s still giving a problem check this image.
As you can see the post 172 finishes but the rank still continues
You could use the following statement but the performance will be abissimal on any large table.
For each record, a subselect is executed that get’s the
countas a rank for equalpostid‘s where the primary key is less or equal to that of the current record. If you return 100 records, in essence 101 statements would have been executed.SQL Statement (slow)
SQL Statement (fast(er))