select * from (
SELECT
r2.`id`,
r2.`photo_id`,
case
when
case when r2.`photo_id` <> @grupa then 1
else 0 end = 1 then @rownum:= 0
else @rownum:= @rownum
end as tr,
@rownum:= @rownum+ 1 AS rank,
@grupa := r2.`photo_id` as gr
FROM
`photos_comments` r2, (SELECT @rownum := 0, @grupa := '', @grupa ) r
GROUP BY
r2.`photo_id`,
rank
) as tab_rank, `photos`
where (tab_rank.rank <=5) and (`photos`.`id` = tab_rank.`photo_id`)
Na stronie http://forums.devshed.com/mysql-help-4/mysql-rank-function-487657.html znalazłem coś takiego:
SET @rank = 1, @prev_val = NULL, @prev_rank = NULL;
SELECT rank FROM
(
SELECT @rank := IF(@prev_val!=uXP,@prev_rank+1,@rank) AS rank
, @prev_val := uXP AS uXP
, @prev_rank := @rank AS prevRank
, t.uID
, t.uName
FROM
(
SELECT uID, uName, SUM(tblbase.basXP) AS uXP
FROM tbluser INNER JOIN tblbase ON tbluser.uID = tblbase.basUserID
GROUP BY tblbase.basUserID
ORDER BY uXP DESC
) AS t
) AS showRank WHERE uID = 5
To chyba esencja tego czym można określić miano Rank dla MySQL
Brak komentarzy:
Prześlij komentarz