Ronald
Ronald

Reputation:

How do I ignore a single row in a query result using SQL or PHP?

Can I somehow ignore the post with the earliest date? With SQL or PHP?

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

Cheers

Upvotes: 1

Views: 1070

Answers (4)

gargantuan
gargantuan

Reputation: 8944

I've got nothing to test this with, but would this work?

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

Or for MySQL five compatibility as pointed out in the comments

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

The large number was copied exactly from the MySQL docs.

Upvotes: 2

Eric Petroelje
Eric Petroelje

Reputation: 60498

You could probably write some convoluted sql to do it, or you could just do it in your php:

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

    // Process rows here
}

Upvotes: 3

Scott Ivey
Scott Ivey

Reputation: 41558

If you're using SQL 2005 or better, you could use the row_number function...

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

Upvotes: 0

Steve Broberg
Steve Broberg

Reputation: 4394

Assuming date uniquely determines a row for a given 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

Upvotes: 1

Related Questions