Switch content of 2 rows?

Discussion in 'Databases' started by Teunel, Jun 1, 2008.

  1. #1
    Hi,

    First of all: I have almost no knowledge of phpMyAdmin, but I have a little dillema.

    I have a database, and in the database is a table with 6499 records. What I want to do is switch the content from one row (Don't know if this is the correct word for it) to another row.

    I'll try to explain it with a screenshot.
    [​IMG]

    I want to change the 2 rows with the arrow.
    Is this possible somehow?
     
    Teunel, Jun 1, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Do you want to change only the contents or the locations?
     
    mwasif, Jun 1, 2008 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    It is possible by the following procedure (make sure you backup your data before performing these steps or first test this on your test data)

    Step 1 - Create a temporary field
    ALTER TABLE table_name ADD `temp_field` TEXT NOT NULL ;
    Code (markup):
    Step 2 - Copy one column's data to this temporary field
    UPDATE table_name SET temp_field = subject;
    Code (markup):
    Step 3 - Copy 2nd column's data to subject field
    UPDATE table_name SET subject = body;
    Code (markup):
    Step 4 - Copy temporary field contents (which is subject data in real) to body field
    UPDATE table_name SET body = temp_field;
    Code (markup):
    After completing step 4, you have switched the both columns data. Make sure you have successfully switched data. If everything is fine then you can remove temp_field column without any problem.

    You may loss some data if you subject and body are not of same data type and have more data the other column can save.

    Note: Replace table_name with your actual table name.
     
    mwasif, Jun 1, 2008 IP
    Teunel likes this.
  4. Teunel

    Teunel Guest

    Messages:
    246
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Wow, thank you so much! You have no idea how much you helped me with this! +rep added! :D
     
    Teunel, Jun 1, 2008 IP