简体   繁体   English

MySql 选择具有开始和条件的特定行数

[英]MySql select specific count of rows with start and condition

I want to select a specific count of rows from MySql from an specific startpoint, with a condition.我想从特定起点从 MySql 中选择特定数量的行,并带有条件。 The startpoint should be a variable.起点应该是一个变量。 My problem is that I dont have the row where to start.我的问题是我没有从哪里开始的行。

In Words: "Start after the first 5 rows where "status" is equals 1 and give me from there the next 5 rows whith "status" equals 1".用字词:“从“状态”等于 1 的前 5 行开始,然后从那里给我“状态”等于 1 的下 5 行”。

Example Data:示例数据:

-----------------------
| id | title | status |
-----------------------
| 1  | Test  |    1   |
| 2  | Test  |    0   |
| 3  | Test  |    1   |
| 4  | Test  |    0   |
| 5  | Test  |    1   |
| 6  | Test  |    1   |
| 7  | Test  |    0   |
| 8  | Test  |    1   |
| 9  | Test  |    1   |
| 10 | Test  |    1   |
| 11 | Test  |    1   |
| 12 | Test  |    1   |
| 13 | Test  |    0   |
| 14 | Test  |    1   |
| 15 | Test  |    0   | 

First Case第一种情况

I want five rows with status equals 1, started at the beginning.我想要五行状态等于 1,从头开始。 (The start variable is 0). (起始变量为 0)。

Expected Data:预期数据:

-----------------------
| id | title | status |
-----------------------
| 1  | Test  |    1   |
| 3  | Test  |    1   |
| 5  | Test  |    1   |
| 6  | Test  |    1   |
| 8  | Test  |    1   |

Second Case第二种情况

Now I need the next 5 rows where status is equals 1. (The start variable is 5).现在我需要接下来的 5 行,其中状态等于 1。(起始变量是 5)。

Expected Data:预期数据:

-----------------------
| id | title | status |
-----------------------
| 9  | Test  |    1   |
| 10 | Test  |    1   |
| 11 | Test  |    1   |
| 12 | Test  |    1   |
| 14 | Test  |    1   |

How can i achieve this?我怎样才能做到这一点?

Use the LIMIT clause.使用 LIMIT 子句。 The first parameter is the index in the complete result set from where you want to start receiving results.第一个参数是完整结果集中的索引,您希望从哪里开始接收结果。 The second parameter is the number of results you want.第二个参数是您想要的结果数。 So to get the first 5 results:因此,要获得前 5 个结果:

SELECT * FROM my_table WHERE status = 1 ORDER BY id LIMIT 0,5

To get the next 5:要获得接下来的 5 个:

SELECT * FROM my_table WHERE status = 1 ORDER BY id LIMIT 5,5

And so on.等等。 Also see here: http://dev.mysql.com/doc/refman/5.5/en/select.html另见此处: http : //dev.mysql.com/doc/refman/5.5/en/select.html

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

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