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

I have created a question Here That I think is a little bloated. To skim down this question here is my requirements:

I need to create 3 mySQL queries that grab all information in that row so that I can format the information into one of HTML tables.

Here is the query that I have which selects everything:

$qry_questions = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC);

while($row = mysql_fetch_array($qry_questions){

//Grab all information

}

Now, I need to make three queries that act like the one above, but I need it to do something like this:

pull everything from every third row in a table starting at row #1

pull everything from every third row in a table starting at row #2

pull everything from every third row in a table starting at row #3

Then I would put each of those queries into one of the 3 columns.

I can not do this by unique ID. Yes, it is an auto incrementing ID, but it will be probable that I might have to move whole rows into another table.

EDIT: I've added my attempt to count each row and place the queried result into the right "bin"

//GIVE EACH ENTRY A COLUMN NUMBER

$count++;

if($count >= 4){

$count = 1;

}?>

while($count == 1){

include("pieces/answers/newAnswerLayout.php");

}

?>

while($count == 2){

include("pieces/answers/newAnswerLayout.php");

}

?>

while($count == 3){

include("pieces/answers/newAnswerLayout.php");

}

?>

解决方案

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

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值