順位を取得。※但し参照するレコードが増えるとクソ重い
pointフィールドを元に、降順で自分の順位を求める。※存在しないidでも_rank_が1になるので要注意
▼pointが同値の場合、タイ扱い
SELECT count(*)+1 AS _rank_ FROM HOGE_TABLE WHERE point > (select point FROM HOGE_TABLE WHERE id = ?);
▼pointが同値の場合、DISTINCTでまとめて重複を除外
SELECT count(DISTINCT point)+1 AS _rank_ FROM HOGE_TABLE WHERE point > (select point FROM HOGE_TABLE WHERE id = ?)
指定順位(5〜10位)のランキングを降順で取得
実装する場合は順位をプレースホルダにすればいいよ。
▼pointが同値の場合、タイ扱い
SELECT tmpTable1.id, tmpTable1.point, ( SELECT count(tmpTable2.point) FROM HOGE_TABLE as tmpTable2 WHERE tmpTable2.point > tmpTable1.point )+1 as _rank_ FROM HOGE_TABLE as tmpTable1 WHERE ( SELECT count(tmpTable2.point) FROM HOGE_TABLE as tmpTable2 WHERE tmpTable2.point > tmpTable1.point )+1 >= 5 AND ( SELECT count(tmpTable2.point) FROM HOGE_TABLE as tmpTable2 WHERE tmpTable2.point > tmpTable1.point )+1 <= 10 ORDER BY tmpTable1.point DESC
▼pointが同値の場合、DISTINCTでまとめて重複を除外
SELECT DISTINCT tmpTable1.point, ( SELECT count(DISTINCT tmpTable2.point) FROM HOGE_TABLE as tmpTable2 WHERE tmpTable2.point >= tmpTable1.point ) as _rank_ FROM HOGE_TABLE as tmpTable1 WHERE ( SELECT count(DISTINCT tmpTable2.point) FROM HOGE_TABLE as tmpTable2 WHERE tmpTable2.point > tmpTable1.point )+1 >= 5 AND ( SELECT count(DISTINCT tmpTable2.point) FROM HOGE_TABLE as tmpTable2 WHERE tmpTable2.point > tmpTable1.point )+1 <= 10 ORDER BY tmpTable1.point DESC