开发者

Eliminate duplicate field combinations

开发者 https://www.devze.com 2023-03-29 09:35 出处:网络
I want to eliminate duplicate field1/field2 combinations in my DB. My pseudo query below: SELECT * WHERE DISTINCT field1 AND field2

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消