Forum Moderators: coopster

Message Too Old, No Replies

Grabbing all but first 7 rows using mysql

         

twist

12:08 am on Jul 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I was going to grab the first 7 rows from a table I would use LIMIT 7, but how do I go about grapping all the rows but the first 7?

Brief description of my problem,

I have an archive of news articles. I keep the seven most recent articles on the front page of my website. I don't want the articles on the front page appearing in the archive until after they are no longer on the front page (no dupe content). So I want to remove the first 7 articles from the archives. I would like to accomplish this within the mysql query if possible.

jatar_k

12:15 am on Jul 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



[dev.mysql.com...]

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

mysql> SELECT * FROM table LIMIT 95,18446744073709551615;

twist

12:42 am on Jul 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the info jatar, I was hoping there was some type of EXCLUDE syntax but that will definately do the trick.

twist

2:52 am on Jul 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I thought it worked, or at least it worked on one section.

Here is some more detail,

My archives are divided by months, Jan-Dec, so their is always going to be a rollover between months. Using LIMIT, took out the first 7 results for each month. I could use php to check for month and month rollover and then base the query on that, but it's going to require a lot of overhead.

So, is there a way to do a query on a table but leave out the last 7 rows of the table from the query itself?

If I did a row count query and minused seven from that and then used that number in the query like, WHERE row_num != $row_10 AND row_num != $row_11 and so on, would that be a bad idea?

ergophobe

3:03 pm on Jul 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want to keep out the last seven, reverse your sort order and make them the first seven, as in

ORDER BY pubdate
ORDER BY pubdate DESC

whichever. If you want the headlines to appear in the opposite order from the query sort, just concatenate differently.

$headlines .= $this_headline; // same order as query
$headlines = $this_headline . $headlines; // reverse order

Will that work for you?


If I did a row count query and minused seven from that and then used that number in the query like, WHERE row_num!= $row_10 AND row_num!= $row_11 and so on, would that be a bad idea?

Probably a bad idea. Presumably row_num would be an auto_incremented primary key? If so, it could be anything and need not be sequential numbers. Better to use a limit clause. You could get a row count and use

$limit = $row_count - 7;

... LIMIT 7, $limit