Change the order of database items

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
badihi
Forum Newbie
Posts: 15
Joined: Sun Jul 05, 2009 1:16 am

Change the order of database items

Post by badihi »

Hello Friends.
How I can change the order of database items.
For example I have a table like this:

[text]Name Email
----------------------------
John john@ex.com
Peter peter@ex.com
Sam sam@ex.com[/text]

Now I want to change the order of this items. For example I want to change the above table to this:

[text]Name Email
----------------------------
Peter peter@ex.com
Sam sam@ex.com
John john@ex.com[/text]

How I can do this?
Thanks!
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Change the order of database items

Post by alex.barylski »

Dump the data, re-order the array and re-insert. Alternatively, you can add a field, such as `offset` and in your scripts include an ORDER BY clause to effectively order your result set.

Cheers,
Alex
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: Change the order of database items

Post by Gargoyle »

Code: Select all

ALTER TABLE [table] ORDER BY eMail DESC
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Change the order of database items

Post by AbraCadaver »

Why not just order it when you select it?


[text]SELECT * FROM table_name ORDER BY Email DESC[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
badihi
Forum Newbie
Posts: 15
Joined: Sun Jul 05, 2009 1:16 am

Re: Change the order of database items

Post by badihi »

Thanks for your replies, friends.
I think that using 'ORDER BY' is the best way. It seems that there is no way to change item's order.
Any way, thanks for your helping! And sorry for my bad English!
Post Reply