Forum Moderators: coopster

Message Too Old, No Replies

Searching Backwards

         

Schoolbag

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

10+ Year Member



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.

jusdrum

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

10+ Year Member



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!

Schoolbag

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

10+ Year Member



Yes, I have that in my query.

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

thanks.

coopster

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

WebmasterWorld Administrator 10+ Year Member



It returns the data in that particular order.

Schoolbag

10:23 pm on Sep 2, 2004 (gmt 0)

10+ Year Member



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?

jusdrum

10:26 pm on Sep 2, 2004 (gmt 0)

10+ Year Member



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.