Results 1 to 3 of 3

Thread: merge rows within the same table

  1. #1
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    53
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default merge rows within the same table

    good day everybody,.

    my problem is this:


    |----------------------------------|
    | col1 | col2 | col3 | col4 | col5 |
    |----------------------------------|
    | val1 | val2 | val3 | NULL | val5 |
    | val1 | val2 | val3 | val4 | NULL |
    | val6 | val7 | val8 | val9 | NULL |
    | val6 | val7 | val8 | NULL | val0 |
    | vala | valb | valc | vald | NULL |
    |----------------------------------|


    OUTPUT:

    |----------------------------------|
    | col1 | col2 | col3 | col4 | col5 |
    |----------------------------------|
    | val1 | val2 | val3 | val4 | val5 |
    | val6 | val7 | val8 | val9 | val0 |
    | vala | valb | valc | vald | NULL |
    |----------------------------------|


    just using SELECT,. they are just in one table
    hope you understand,.




    thx in advance,.

    more power to y'all





    -regicide

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    254
    Thanks
    1
    Thanked 20 Times in 20 Posts

    Default

    Sorry, but I don't think anyone understands since you didn't tell us what the issue is or what you are trying to do. Explain better please.

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    You'll need to do 3 things:
    1. Create new_table from the unique values of old_table
    2. Delete old_table
    3. Rename new_table old_table

    The SQL would look something like:

    Code:
    CREATE TABLE new_table SELECT * FROM old_table WHERE 1 GROUP BY col1
    Code:
    DROP TABLE old_table
    Code:
    RENAME TABLE new_table TO old_table
    Since you're dealing with multiple columns, though, using DISTINCT may be the way to go:

    Code:
    SELECT DISTINCT col1, col2, col3, col4, col5 FROM table1
    Hopefully that helps.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

Bookmarks

Posting Permissions

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