如何在mysql中x行数之后选择行?

时间:2017-02-08 13:04:57

标签: mysql

假设我有:

 __________________________________
| ID |   NAME   |       ITEM        |
-----------------------------------
| 2 |   John   |        vlc        |
````````````````````````````````````
| 3 |   mike   |        vlc        |
````````````````````````````````````
| 4 |   ali    |        vlc        |
````````````````````````````````````
| 5 |   Insan  |        vlc        |
````````````````````````````````````
| 6 |   Hawai  |        vlc        |
````````````````````````````````````
| 7 |   Jack   |        vlc        |
````````````````````````````````````
| 8 |   Chan   |        vlc        |
````````````````````````````````````

现在我有一个查询:

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 2

您可能知道这会选择第一行和第二行,因为我将其限制为最多选择两行,因此结果为JohnMike

问题:

我想要的是在两个第一行之后选择2行,例如aliInsan。 如:

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 2 but after 2 first rows

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 2 but after 5 first rows

感谢所有事情:)

3 个答案:

答案 0 :(得分:3)

使用OFFSET

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 2 OFFSET 5

您可以使用不带OFFSET的简短查询和使用逗号的LIMIT,如下所示

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 5,2

请注意,在第二种方法中,首先是OFFSET值,然后是LIMIT值

答案 1 :(得分:1)

您也可以使用

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 2, 2

答案 2 :(得分:0)

在mysql中,您可以使用LIMIT with OFFSET

SELECT NAME FROM table WHERE ITEM='vlc' LIMIT 2 OFFSET 2

这将返回以第三个值

开头的2个值