-1

I'm totally new in SQL. I never used it and just need a simple answer because I don't have time to learn SQL right now :(. I need to remove duplicated records from my local DB. Case looks like this:

| id | type  | ... |
-------------------
| 1  | test  | ... |
| 1  | test2 | ... |
| 1  | test  | ... |
| 1  | test  | ... |
I want to remove all duplicated record which has the same id and type but leave only on record. Like this:

| id | type  | ... |
-------------------
| 1  | test  | ... |
| 1  | test2 | ... |

Using delete by Id is impossible. I have 50k records and I want to remove all duplicated records. When ID and Type are the same.

tarnasm
  • 21
  • 4

5 Answers5

1

Please try this

First Way

 SELECT id, type
        FROM table_name
  Group by id, type

Second Way

 SELECT DISTINCT id, type
        FROM table_name;
Hemang A
  • 1,012
  • 1
  • 5
  • 16
0

SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

SELECT DISTINCT column1, column2, ...
FROM table_name;

In your table

 SELECT DISTINCT id, type, ...
    FROM table_name;
Fatikhan Gasimov
  • 903
  • 2
  • 16
  • 40
0

you just need to use the keyword distinct when selecting mate.. try like this

 SELECT DISTINCT id, type, blah blah blah FROM your_table; // this should take care of em
ALPHA
  • 1,135
  • 1
  • 8
  • 18
0

A TSQL sample code that might help:

 WITH tbl_alias AS
    (
       SELECT id, type,
           RN = ROW_NUMBER()OVER(PARTITION BY id, type ORDER BY id)
       FROM tbl
    )
    DELETE FROM tbl_alias WHERE RN > 1

Also you can try How to delete duplicates on a MySQL table? .

Amir Molaei
  • 3,700
  • 1
  • 17
  • 20
0

You should replace your table grouping by id and type, and using an aggregate function on the other fields.

You should add to your question the definition of your table and specify the rule to use to get the other fields. Anyway, this is a simple solution:

-- Create a temp copy of original table with distinct values
CREATE TEMPORARY TABLE copy_table1 
   SELECT id, type, MIN(field3) AS field3, ...
   FROM table1
   GROUP BY id, type;

-- Empty original table
DELETE FROM table1;

-- Insert distinct data into original table
INSERT INTO table1 (id, type, field3, ...) 
    SELECT id, type, field3, ...
    FROM copy_table1;
kiks73
  • 3,718
  • 3
  • 25
  • 52