Remove duplicates from a table column in database

You have a table with a column with many duplicates and you just want to keep one of each. Make a distinct copy of the table, remove the old table and rename the new one.

CREATE TABLE temp_table as SELECT * FROM duplicate_table WHERE 1 GROUP BY duplicate_column;

DROP TABLE duplicate_table;

RENAME TABLE temp_table TO duplicate_table;

This example sumarize a value when aggregate to duplicates

CREATE TABLE temp_table as SELECT wid, word, SUM(count) as count FROM words_1000 WHERE 1 GROUP BY word;

DROP TABLE words_1000;

RENAME TABLE temp_table TO words_1000;

If you have a table with duplicates an you whant to aggregate the column "word" but keep the "count" value by summarize it.

before:
wid word count
438 adressen 135
932 aldrig 321
456 alfta 100
567 adressen 100
678 aldrig 200
789 alfta 26

after:
wid word count
438 adressen 235
932 aldrig 521
14260 alfta 126

INSERT from SELECT in MySQL

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

Replace string in database with REPLACE() function.

Knowledge keywords: