How to choose TOP 5 and then the next 5?

since comments always have it, you can choose to show the 5 best comments (which I know how), and if there are more than 5, you can click the link and the following 5 comments will be displayed in it. The problem is that I don't know what the code should look like for the next 5.

The best idea of ​​what I say is, let's say I have 10 comments, I use

SELECT * FROM news ORDER BY ID DESC LIMIT 5

which will show TOP 5 comments, say comments with ID 10,9,8,7,6

but what if I need comments with ID 5,4,3,2,1?

+3
source share
5 answers
SELECT * FROM news ORDER BY ID DESC LIMIT 5, 5

, MySQL, LIMIT : offset () row_count:

[LIMIT {[offset,] row_count | row_count}]

offset - PostgreSQL.

+4

, MySQL, :

SELECT * FROM news ORDER BY ID DESC LIMIT 5 OFFSET 5
+2

, , :

SELECT TOP 5 *
  FROM table
 WHERE pk NOT IN (SELECT TOP (page * 5) pk
                    FROM table
                   ORDER BY pk)
 ORDER BY pk
+1

.

5, . : , .

+1

( Joscha), , , SQL-, .

, :

SELECT id, title, body, date FROM news ORDER BY id DESC LIMIT 10

And then collapse your result. Display the first five except the following five.

When you want to have something like a Paginator, how can you start, say, page 0:

$page = isset($_GET['page'] ? intval($_GET['page']) : 0;
SELECT id, title, body, date FROM news ORDER BY id DESC LIMIT 5 OFFSET $page * 5 + 5;
0
source

Source: https://habr.com/ru/post/1737805/


All Articles