1. 程式人生 > >去除mysql表中重複的的資料

去除mysql表中重複的的資料

由於併發,導致資料庫中同樣的資料多了幾份,所以需要清楚資料。比如去除person表中重名的資料(實際中判斷行資料相同可能通過多個欄位判斷,這裡只是通過name來確定)

一、將名字為張三資料去重;

delete from person where name='張三' and id not in (select max(id) id from person where name='張三' group by name having count(name) > 1)

1、先查詢出姓名為張三的所有資料,然後分組,分組的資料大於1的說明有重複的,這個根據需要保留max或者min那條資料隨意選擇;

2、在相同的條件下,刪除重複的資料排除max或min那條;

二、全表去重;

delete from person where id not in (select max(id) id from person group by name having count(name) > 1)