Welcome to WebmasterWorld Guest from 54.227.14.23

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Searching Backwards

     
9:59 pm on Sep 2, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


Hi,

Id like to query a MySQL database with PHP but tell it to start querying from the last record and go backwards.

I know how to get the last line from a query, but this database may be very large, and the data that is needed will always be newly entered info, thus, at the end.

10:02 pm on Sept 2, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 29, 2004
posts:194
votes: 0


In your SQL query, add in an ORDER at the end, and order it by something that you know will make the records be in reverse order (such as an auto incrementing numerical ID or date the record was entered).

Example:

select * from mytable where field like '%blah%' order by ID desc limit 0,30

The limit parameter will also help you limit how many rows are returned, saving a bit of resources.

Hope this helps!

10:09 pm on Sept 2, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


Yes, I have that in my query.

But does that actually search backwards, or does it just return your data in reverse order?

thanks.

10:16 pm on Sept 2, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


It returns the data in that particular order.
10:23 pm on Sept 2, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


If I have a large file, lets say a Gigabyte, and I need to access the last line isnt it better to query backwards since I know the data will be at the end? or is this really a non-issue my Mysql?
10:26 pm on Sept 2, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 29, 2004
posts:194
votes: 0


It kind of isn't an issue in MySQL, but a lot of rows in a table can slow performance. You can index fields in MySQL to make your queries run faster. If there is a field you are searching consistently, try indexing it and it will make your queries run faster.