Eliminate duplicate field combinations
I want to eliminate duplicate field1/field2 combinations in my DB. My pseudo query below:
SELECT * WHERE DISTINCT field1 AND field2
The table with duplicates is below:
+-----+--------+--------+--------+
| id | field1 | field2 | field3 |
+-----+--------+--------+--------+
| 1 | A | B | Z |
| 2 | A | B | Q |
| 3 | A | C | K |
+-----+--------+--------+--------+
I want to eliminate rows with duplicate field1/field2 combinations. Row 2 was dropped b/c it was a duplicate of row 1.
+-----+--------+--------+--------+
| id | field1 | field2 | field3 |
+-----+--------+--------+--------+
| 1 | A | B | Z 开发者_JAVA百科 |
| 3 | A | C | K |
+-----+--------+--------+--------+
This select will narrow your results as you want:
select id, field1, field2, field3 from testtable1 group by field1,field2;
So this will create a new table with the duplicates eliminated:
create table testtable2 select id, field1, field2, field3 from testtable1 group by field1,field2;
Replace testtable1
with the appropriate name of your existing table.
精彩评论