简体   繁体   English

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

[英]How to select every nth row in mySQL starting at 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. 我需要创建3个mySQL查询,以获取该行中的所有信息,以便可以将信息格式化为HTML表之一。

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. 然后,我将每个查询放入3列之一。

I can not do this by unique ID. 我无法通过唯一ID来执行此操作。 Yes, it is an auto incrementing ID, but it will be probable that I might have to move whole rows into another table. 是的,它是一个自动递增的ID,但是可能我可能必须将整行移动到另一个表中。

EDIT: I've added my attempt to count each row and place the queried result into the right "bin" 编辑:我已经添加了尝试对每一行进行计数并将查询的结果放入正确的“ bin”中的尝试

//GIVE EACH ENTRY A COLUMN NUMBER
$count++;
if($count >= 4){
    $count = 1; 
}?> 

<div class="col" id="col-1">
    <?PHP
    while($count == 1){
        include("pieces/answers/newAnswerLayout.php"); 
    }
    ?>
</div>
<div class="col" id="col-2">
    <?PHP
    while($count == 2){
        include("pieces/answers/newAnswerLayout.php"); 
    }
    ?>
</div>
<div class="col" id="col-3">
    <?PHP
    while($count == 3){
        include("pieces/answers/newAnswerLayout.php"); 
    }
    ?>
</div>

Here's one approach, to get the resultset returned by MySQL. 这是一种获取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. 但这可以在MySQL中轻松完成。 Note that your original query is wrapped in parens (as an inline view) aliased as r . 请注意,您的原始查询包含在别名为r parens中(作为内联视图)。

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). 要使第二行和第二行从第三行开始,请将HAVING子句中的文字= 1替换为= 2= 3

Why do you need three different queries? 为什么需要三个不同的查询? Can you not, instead, keep the single query you have, have a counter that is incremented in every iteration of the loop, and on %3 does whatever you would do for cases #1, #2 or #3? 相反,您是否可以不保留单个查询,是否有一个在循环的每次迭代中都递增的计数器,并且在%3上会执行情况#1,#2或#3的操作?

you can probably just use a counter to do it in a much simpler fashion, but I would do it like this: 您可能可以使用计数器以更简单的方式进行操作,但是我会这样做:

$questions = array();
$results = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC");

while($row = mysql_fetch_array($results)){
    $questions[] = $row;
}

for($i = 0; $i < count($questions); $i++) {
  if($i + 1 % 3 == 0) {
     $questions[$i];//this is a 3rd, so do something with it
  }
}

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

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