Results 1 to 4 of 4
  1. #1

    Is this possible with mysql/php easily?

    I have a column in mySQL:

    ID : auto_increment : PRIMARY-Unique

    Say I have rows

    ID -- NAME
    1 -- A
    2 -- B
    3 -- C
    4 -- D
    5 -- E
    6 -- F
    7 -- G

    And I want to insert a new row after 1 and have mysql shift all the other values so my table looks like

    ID -- NAME
    1 -- A
    2 -- INSERTED
    3 -- B
    4 -- C
    5 -- D
    6 -- E
    7 -- F
    8 -- G

    --------------
    I'm make a sort of filebrowsing system where users can reorganize the files and it stores them in php/mysql, I'm trying to figure out a way to order them so when they refresh/come back it doesn't reset.

    Thanks,
    Robert
    MSN Use the board link under profile!
    Email me through this board!
    Why? So my email isn't harvested!

  2. #2
    Join Date
    Jan 2005
    Location
    Baghdad, Iraq
    Posts
    172
    you want to instert at id (X)
    so use this query:
    $query="UPDATE table SET id=id+1 where id>=X order by id DESC";
    then apply the insert query with your specified id
    wish this help
    Oras
    The Dream is the blueprint of success, the hope is the budget and hard working is the achievement

  3. #3
    Ohh sweet, I didn't know I could do math directly in mySQL;

    Thanks a ton!
    MSN Use the board link under profile!
    Email me through this board!
    Why? So my email isn't harvested!

  4. #4
    You should add another field which would serve for sorting purposes. It's not good to tamper with unique keys, they serve to uniquely identify a row and not for using them for re-ordering rows. If it's sorting all you require, add an extra field (named name_sort or whichever you like, integer type) and operate on that field, without touching primary key. Just a suggestion opposed to Oras's solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •