1

从我的数据库中选择某些内容后,我试图仅显示每第二行。

我的选择如下所示:

SELECT * FROM table where partnerID = '1'

现在从该选择查询的结果中,我只想获取每第二行。

那可能吗 ?

4

4 回答 4

6
$count = 0;
while($row = mysql_fetch_array($results)) {
    $count++;
    if($count % 2 == 1) continue;

    // What you want to do with the rows you don't want to skip here...
}

附带说明一下,您可以使用相同的策略仅显示每 3 行或每 4 行,只需更改在模运算符旁边放置的数字

if($count % 3 != 0) continue; // Show only every third row
if($count % 4 != 0) continue; // Show only every fourth row
于 2012-05-22T15:48:45.203 回答
6
SELECT * FROM (
    SELECT  @n := @n + 1 AS position,
            t.*
    FROM (SELECT @n:=0) counter,
         table t
    WHERE partnerID = 1)
AS query
WHERE MOD(position,2) = 0
于 2012-05-22T15:52:56.523 回答
5

您可以通过以下方式执行此操作:

SELECT * FROM
(
    SELECT
      @I := @I + 1 AS rowNumber,
      tablename.*

    FROM
      tablename,
      (SELECT @I := 0) VT1

    WHERE partnerID = 2

    ORDER BY ID
) NumberedRows
WHERE MOD(rowNumber, 2)=0
;
于 2012-05-22T15:58:00.847 回答
0

不确定这是否是正确的答案,但看起来你需要使用类似 MOD 函数的东西(http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod

SELECT * FROM `table` WHERE id & 1;

将返回奇数行

于 2012-05-22T15:48:31.650 回答