tags:

views:

91

answers:

2

I'm dealing with a legacy database table that has no insertion date column or unique id column, however the natural order of insertion is still valid when examined with a simple SELECT * showing oldest to newest.

I'd like like to fetch that data with pagination but reverse the order as if it was ORDER BY date DESC

I've thought about wrapping the query, assigning a numeric id to the resulting rows and then do an ORDER BY on the result but wow that seems crazy.

Is there a more simple solution I am overlooking?

I cannot add columns to the existing table, I have to work with it as is.

Thanks for any ideas!

+1  A: 

If you're writing an application to process the data, another approach might be to run your current query, then iterate over the returned records from last to first.

If you have too many records, then you may wish to instead use a view. This is a Database object which can be used to combine data from different tabls, or present a modified view of a single table, amongst other things. In this case, you could try creating a view of your table and add a generated ID column. You could then run SELECT statements against this view ordering by the new column you have added.

However be aware of the advice from another poster above: the order in which rows are returned without an ORDER BY clause is arbitrary and may change without notification. It would be best to amend your table if at all possible.

mySQL CREATE VIEW syntax

Mark Chorley
Right, per my musings near the end of the original question. But see here's the thing, there may be hundreds, thousands of entries while I am mostly interested in displaying the last few dozen entered. I could use mysql variables and do a counter for each row and then wrap those rows with the paginated query and ORDER DESC but that completely defeats efficiency and what I am specifically trying to avoid.
_ck_
Maybe use a view to add the generated key if you really can't alter the original table.
Mark Chorley
Could you please expand on that Mark? I am not familiar with the term "view" relative to mysql?
_ck_
I've expanded my answer with some details of Views
Mark Chorley
Ah thank you. But I am also restricted to mysql 4 compatibility unfortunately.
_ck_
+2  A: 

Simply use @rownum in your query to number each row and then order by the @rownum desc. Here's an example.

select @rownum:=@rownum+1 ‘rank’, p.* from player p, (SELECT @rownum:=0) r order by score desc limit 10;

Finally, beware that relying on the current order being returned long-term isn't recommended.

Cory House
Ah this is a practical example of what I was musing about in the middle of my question. Thank you. What I fear though is the table could be quite large at some point and looping through all rows might take quite a performance hit. Still, it might be the only way.
_ck_
The solution I'm suggesting requires no looping. The data is pulled straight out of the DB in the desired order.
Cory House
Yup I understand. And I just tested it and it seems to work well. What I meant by looping is that mysql will have to pull ALL the rows to number them and then get to the last 10 (for example, if that's the desired pagination).
_ck_