如何使用SQL或PHP忽略查询结果中的单行?

时间:2009-06-26 18:46:51

标签: php mysql

我可以以某种方式忽略最早日期的帖子吗?使用SQL还是PHP?

SELECT subject, date 
FROM posts
WHERE id = $id
ORDER BY date DESC
while ($row = mysql_fetch_array($query)) {

干杯

4 个答案:

答案 0 :(得分:3)

你可能会写一些复杂的sql来做,或者你可以在你的php中做到这一点:

$first = true;
while ($row = mysql_fetch_array($query)) {
    if ( $first ) {
       $first = false;
       continue;
    }

    // Process rows here
}

答案 1 :(得分:2)

我没有什么可以测试的,但这会有效吗?

SELECT subject, date 
FROM posts
WHERE id = $id
OFFSET 1
ORDER BY date DESC

或者在评论中指出的MySQL五兼容性

SELECT subject, date 
    FROM posts
    WHERE id = $id
    LIMIT 1,18446744073709551615;
    ORDER BY date DESC

大量数据完全是从MySQL文档中复制而来的。

答案 2 :(得分:1)

假设日期唯一地确定给定id的行,

  SELECT subject, date 
    FROM posts
   WHERE id = $id
     AND date NOT IN
         ( SELECT MIN(date)
             FROM posts
            WHERE id = $id
         )
ORDER BY date DESC

答案 3 :(得分:0)

如果您使用的是SQL 2005或更高版本,则可以使用row_number函数...

With MyCTE AS (
    SELECT subject, 
           date,
           RowNumber = ROW_NUMBER() OVER(ORDER BY date DESC)
    FROM   posts
    WHERE  id = $id)
SELECT *
FROM   MyCTE
WHERE  RowNumber > 1
ORDER BY date DESC