简体   繁体   中英

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

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
}

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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