Jump to content

desc results without order by?


MrAdam

Recommended Posts

by default the results returned from an sql query are selected from the start of the database, to the end. If that makes sense? .. Basically the results are returned ASC by default?Can you change the query to select from the end of the database to the start, so they are effectively ordered DESC? (Without having to specify a field to order by)If not, I think i may face a problem.... see i created a website quite a while ago that has products in the database and a unique ID that isn't incremental, the owner uses his own ordering system. So I cannot make the query like:"SELECT..........ORDER BY id DESC"if i want to select the last 5 entries.... could anyone think of a way of doing it? anyway i could alter the table or anything?- Cheeerrs!

Link to comment
Share on other sites

There's not really a way to do that after the fact. You would need to have had a date field in the table the records when each row was added, or something else that would get incremented. If you don't have a field in the database that is ordered in the way the records were added then you can't sort by that. You could get the results without sorting, store the rows in a data structure like an array, and reverse the array or go through it backwards.

Link to comment
Share on other sites

Hmm, thanks for the reply but, quite limited in what I can do. See there's already around 300 records in the database... I could pretty easily create a loop to assign each record a sort number, but it won't automatically increment with each new row... thinking about creating a new table and transferring the records, and applying the sort number incrementally.... al give it a try!-cheers!

Link to comment
Share on other sites

  • 2 weeks later...

Just create an auto_increment field in your new table

id INT(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...