Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-ordering values in table

Status
Not open for further replies.

JoSno

Programmer
Apr 11, 2003
45
GB
Hi all,

I have a perfectly normal table of the following pattern:

key (int), values.......

the key is tied into the values though.

The problem is I've got to a stage where the keys are out of order so ever query has to have an "order by" clause in it. To save me effort :)P) is there a way to re-order the values in the table to be in key order?

Cheers!
Jo
 
A database table is a set, not some kind of structured table like a spreadsheet. As such, the order in which the database server stores the records is its business and of no concern of ours.

This is why ORDER BY was created -- to force fetched data to be returned in an arbitrary order. This is especially true when returning records from joined tables.

"ORDER BY" is your friend -- use it.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Couldn't have said it better myself sleipnir214.

But I would assume if you insert the keys in order they will come back in order, or if you have an index on the keys and the query uses the index they may come back in that in order.

So while this is not guranteed to work, should not be relied on, why don't you try it and see if you like it.

[tt]
create table mytemp select * from sourceTable orderby MyKey;
drop table sourceTable;
rename table mytemp to sourceTable;
[/tt]

For your purposes this may work, but as sleipnir214 mentioned, this should not be used or relied on.

abombss
 
Thank you for your help abombss, I'll give it a whirl.

sleipnir, i understand entirely what you are saying but if you look at what I said, "To save me effort :)P)" you will see that I am just being lazy and wanted it done for me. I am just concerned that others working from my results may not realise at first they need to apply _order by_ as the "wrong order" only occurs in a couple of places. I suppose that's no concern of mine though.

Jo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top