简体   繁体   English

mysql select 一个条目,然后是接下来的 n 个条目

[英]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.我有一个网页,用户可以从该网页 select 来自 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.显示此条目后,我希望有一个按钮允许用户 select 下五个 DB 条目另一个按钮允许用户 select 前 5 个条目。

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 ,但是两者都存在差距,从表中选择所有数据会导致:

+-----+------------+
| _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它返回 0 条记录,并且

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

which also returns 0 records.它也返回 0 条记录。

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" ):关于使用limitoffset的更详细解释(因为我的评论“不起作用”):

Assume you have a query, with 'lots-of-results', like: SELECT i FROM integers;假设您有一个带有“很多结果”的查询,例如: SELECT i FROM integers; , and you do want to paginate that into pages of 5 records. ,并且您确实希望将其分页为 5 条记录的页面。

When doing: SELECT i FROM integers LIMIT 5 OFFSET 0;执行时: SELECT i FROM integers LIMIT 5 OFFSET 0; will give the first 5 rows.将给出前 5 行。 The first because reading starts from OFFSET 0 .第一个是因为读取从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;接下来 5 条记录可以通过执行SELECT i FROM integers LIMIT 5 OFFSET 5;

Or, in other words, the values after OFFSET should be (pagenummer-1)*5 .或者,换句话说,OFFSET 之后的值应该是(pagenummer-1)*5 With pagenumber=1 is the first page. pagenumber=1 是第一页。

Example to get page 42 ( (42-1)*5=205 ):获取第 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:但是,EXPLAIN 显示出一些有趣的东西:

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.插入的东西是需要读取 210 行才能产生这 5 行结果。

My question had two parts:我的问题有两个部分:

  1. How do I get the next 5 entries?我如何获得接下来的 5 个条目?
  2. how I get the previous 5 entries?我如何获得前 5 个条目?

While I got answers to (1), I had to take parts from various answers to get the result I wanted.虽然我得到了 (1) 的答案,但我不得不从各种答案中分出一部分来获得我想要的结果。 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.其中,根据上述答案之一,我发布了最后显示的条目 $_POST['_id_last'],并将其用作查询的起点。

For (2), which no one addressed, what I came up with was对于(2),没有人提到,我想出的是

$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.我使用子查询获取前 5 个条目并将其包装在一个查询中,该查询按降序对结果进行重新排序。

There may be a more elegant solution for (2), but this is what I came up with and it works for me. (2) 可能有一个更优雅的解决方案,但这是我想出的,它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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