----+-------------+-
1 | 129 |
2 | 10 |
3 | 20 |
4 | 30 |
5 | 40 |
6 | 50 |
7 | 60 |
8 | 70 |
9 | 80 |
10 | 90 |
11 | 100 |
12 | 110 |
13 | 120 |
(13 rows)
というテーブルデータがあるのですが、以下のSQLを流すと
select (select count(*) from total_point h2 where h2.total_point>=h1.total_point) as zyun, total_point from total_point h1 order by zyun offset 0 limit 5;
zyun | total_point
------+-------------
1 | 129
2 | 120
3 | 110
4 | 100
5 | 90
こんな結果になるんですよーー。
出来れば、10を指定したら、そこから上五個表示
下五個表示とするにはどんなSQLが必要ですか?
DBはPostgreSQLです。
SELECT (SELECT COUNT(*) FROM total_point h2 WHERE h2.total_point>=h1.total_point) AS zyun, total_point FROM total_point h1 WHERE zyun>=$num-5 AND zyun<=$num+5 ORDER BY zyun;
WHEREで条件を指定
ありがとうございます。