Log in

View Full Version : merge rows within the same table



regicidedelferoz
07-01-2011, 02:01 AM
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

fastsol1
07-01-2011, 11:58 AM
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.

alexjewell
07-06-2011, 01:53 PM
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:



CREATE TABLE new_table SELECT * FROM old_table WHERE 1 GROUP BY col1



DROP TABLE old_table



RENAME TABLE new_table TO old_table


Since you're dealing with multiple columns, though, using DISTINCT may be the way to go:



SELECT DISTINCT col1, col2, col3, col4, col5 FROM table1


Hopefully that helps.