Jump to content


change database entry order

  • Please log in to reply
2 replies to this topic

#1 tucker

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 December 2005 - 09:49 PM

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.


#2 ryanlwh

  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 08 December 2005 - 04:59 AM

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--]
Please use EDIT * 100...
Please use
or [php] * 1000...


#3 Zup

  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationWinnipeg, MB

Posted 08 December 2005 - 04:37 PM

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.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users