SQL deletes duplicate content in fields and retains unique data

I encountered a very interesting SQL problem. I should also encounter such problems in practical applications.

Record it here to deepen your impression.

Some tables will have some duplicate content, which requires us to remove the duplicate content in the table until only the unique ones remain. Directly upload the screenshot:
original table:
Insert image description here

Target table:
Insert image description here
Remove the duplicate content of the word field in the original table and keep only one, and also keep the data of other fields corresponding to it.

Elementary method one:

First find out the duplicate content in the word field (these content needs to be deleted)

select word from query_test group by  word having count(word)>1;

Then determine the unique value that needs to be retained for these duplicate contents (determined by id)

select min(id) from query_test group by word having count(word)>1;
--word字段重复内容对应的最小id,有几个word重复,对应几个最小id

Finally remove duplicate content and keep unique and non-duplicate content

delete from query_test where word in 
(select word from query_test group by word having count(word)>1) 
and id not in (select min(id) from query_test group by word having count(word)>1);

Mysql may report an error if you write it this way (the update table cannot reference your own table). You need to add an alias to deal with it:

delete from query_test where word in(select b.word from 
(select word from query_test group by word having count(word)>1) b) 
and id not in (select a.id from 
(select min(id) as id  from query_test group by word having count(word)>1)a);

Advanced method

The method proposed by the boss is straightforward.

delete from query_test where id not in (select max(id) from query_test group by word);

Similarly, if it is mysql, it needs to be processed, which will not be shown here.
If there are any problems, you are welcome to point them out, and you are also welcome to suggest other methods to discuss and make progress together.

Guess you like

Origin blog.csdn.net/weixin_55549435/article/details/114057987