Forums

This topic is locked

How can I select all records except the last 10?

Posted 22 Jan 2003 12:22:43
1
has voted
22 Jan 2003 12:22:43 Jon Stanton posted:
I have a page with news items on. It only shows the last 10 items. I then have a link to an archive page which shows all the records. However, I don't want it to show the last 10 as they've already been viewed in the previous page.

Is this easy? I'm using PHP, MySQL and Dreamweaver MX.

Many thanks

Replies

Replied 22 Jan 2003 15:43:56
22 Jan 2003 15:43:56 Owen Eastwick replied:
I don't use PHP but the principal should be the same, provided you can use SELECT COUNT and SELECT TOP with MySQL.



First set up a recordset to cound the number of records:

Recordset1.Source = "SELECT COUNT(AnyField) FROM TableName AS TotalRecords"



Store the result in a variable (varTotalRecords) and calculate the Total records - 10 and store that in a variable (varRecords):

varRecords = varTotalRecords - 10



Now us the variable varRecords to select all but the last 10 records from the table:

Recordset2.Source = "SELECT TOP " & varRecords & " FROM TableName"


LOL - I originally named varRecords v a r S e l e c t R e c o r d s, but the anti obscenity check asterisked out some leters: varSelectRecords, so if you live in Scunthorpe - tough luck. <img src=../images/dmxzone/forum/icon_smile_big.gif border=0 align=middle>

Regards

Owen.

---------------------------------------------------
Used programming books and web development software for sale (UK only):
www.tdsf.co.uk/Shop.htm

Multiple Parameter UD4 / Access 2000 Search Tutorial:
www.tdsf.co.uk/tdsfdemo/

Edited by - oeastwick on 22 Jan 2003 15:50:39
Replied 22 Jan 2003 16:49:13
22 Jan 2003 16:49:13 Jon Stanton replied:
Hi Owen

Thanks for the info. Couldn't find SELECT TOP in the MySQL documentation so assume there isn't one. However, one of my many books showed the LIMIT command which allows an offset and an amount. By sorting the result descending I could offset the LIMIT by 10 with a count of 999 and it shows everything. Job done.

Reply to this topic