Jump to content

Archived

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

tucker

change database entry order

Recommended Posts

I have 40 items listed in a database table...and I need to have items regularly change positions. Is there a way to shift the order of items in a database? Thank you.

 

-Seth

Share this post


Link to post
Share on other sites

you cannot shift the order physically in a table, but you could select the items in random order

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM yourtable ORDER BY rand() [!--sql2--][/div][!--sql3--]

Share this post


Link to post
Share on other sites

If you really need to physically change the orders, you'll have to create a temporary table that has the same properties as the first table. Then you can insert data from the first table into the temporary table, select a row to be moved in the first table, and update the other row (the row that you put into the temporary table) with the information from the selected row. Now select your row from the temporary table, and update the row in the first table that you "moved". If that's too complicated to read, this may help:

 

Let FTable be first table and TTable be temp. table

We will switch rows 2 and 6.

Row 2 from FTable gets copied into TTable.

Row 6 in FTable overwrites Row2 in FTable (select then update)

Row 2 from TTable overwrites Row 6 in FTable (select then update)

TTable is deleted.

End result: row 2 and row 6 are switched.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.