且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从n开始选择mySQL中的第n行

更新时间:2023-12-01 09:34:22

这里是获取MySQL返回的结果集的一种方法. (但是,仅返回所有行并获取应用程序中的每三行可能会更容易).但这可以在MySQL中轻松完成.请注意,原始查询被包裹在别名为(c0>)的parens中(作为内联视图).

Here's one approach, to get the resultset returned by MySQL. (But it might be easier to just return all the rows, and get every third row within the app). But it can be done in MySQL pretty easily. Note that your original query is wrapped in parens (as an inline view) aliased as r.

SELECT r.*
  FROM ( 
         SELECT *
           FROM mbr_qa_questions
          ORDER BY q_votes DESC
       ) r
 CROSS
  JOIN ( SELECT @i := 0 ) s
HAVING ( @i := @i + 1) MOD 3 = 1

这将从第一行开始每隔三行返回一次.要获得从第二行和第三行开始的第三行,请将HAVING子句中的文字= 1分别替换为= 2= 3.

That will return every third row, starting with the first row. To get every third row starting with the 2nd and 3rd row, replace the literal = 1 in the HAVING clause with = 2 or = 3 (respectively).