简体   繁体   中英

mysql select an entry and then the next n entries

I have a webpage from which a user can select an entry from a MySQL DB. After this entry is displayed, I would like to have one button that allows the user to select the next five DB entries and another button that allows the user to select the previous 5 entries.

How can I write the query for these two buttons?

I have a primary key column, _id and a date column date , but there are be gaps in both, ie , selecting all data from the table results in:

+-----+------------+
| _id | date       |
+-----+------------+ 
|   4 | 2020-11-26 | 
|   5 | 2020-11-28 |
|   6 | 2020-11-29 | 
|   7 | 2020-12-01 | 
|   8 | 2020-12-08 | 
|  10 | 2020-12-22 | 
|  12 | 2020-12-25 |
+-----+------------+

For the first button (the next five entries) I have tried:

select * from Blog where post=1 and _id=5 order by date desc limit 5 offset 5;

which returns 0 records, and

select * from Blog where post=1 and _id>=5 order by date desc limit 5 offset 5;

which also returns 0 records.

I don't have a clue how to write the query for the second button, ie the previous five entries.

Any help would be appreciated.

A more detailed explanation on using limit and offset (because the comment I gace "won't work" ):

Assume you have a query, with 'lots-of-results', like: SELECT i FROM integers; , and you do want to paginate that into pages of 5 records.

When doing: SELECT i FROM integers LIMIT 5 OFFSET 0; will give the first 5 rows. The first because reading starts from OFFSET 0 .

mysql> SELECT i FROM integers ORDER BY i LIMIT 5 OFFSET 0;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
+---+
5 rows in set (0.00 sec)

Next 5 records can be obtained by doing SELECT i FROM integers LIMIT 5 OFFSET 5;

Or, in other words, the values after OFFSET should be (pagenummer-1)*5 . With pagenumber=1 is the first page.

Example to get page 42 ( (42-1)*5=205 ):

mysql> SELECT i FROM integers ORDER BY i LIMIT 5 OFFSET 205;
+-----+
| i   |
+-----+
| 205 |
| 206 |
| 207 |
| 208 |
| 209 |
+-----+
5 rows in set (0.00 sec)

But, EXPLAIN is showing something of interest:

mysql> explain SELECT i FROM integers ORDER BY i LIMIT 5 OFFSET 205\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: integers
   partitions: NULL
         type: index
possible_keys: NULL
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 210
     filtered: 100.00
        Extra: Using index
1 row in set, 1 warning (0.00 sec)

The interserting thing is that 210 rows needs to be read to produce this 5 row result.

My question had two parts:

  1. How do I get the next 5 entries?
  2. how I get the previous 5 entries?

While I got answers to (1), I had to take parts from various answers to get the result I wanted. What I came up with was:

$query_blog = 'select * from Blog where _id<'.$_POST['_id_last'].' order by date desc limit 5';

where, per one of the answers above, I POSTed the last displayed entry, $_POST['_id_last'], and used that as the starting point for the query.

For (2), which no one addressed, what I came up with was

$query_blog = 'select * from (select * from Blog where _id>'.$_POST['_id_last'].' limit 5) as subquery order by date desc';

where I used a subquery to get the previous 5 entries and wrapped it in a query that reordered the result in descending order.

There may be a more elegant solution for (2), but this is what I came up with and it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM