今天遇到刪除表中重複的問題。pid為主鍵,但想要刪除menuid相同重覆記錄。剛開始想到的語句是:
delete from qb_product where id not in(select id=min(id) from qb_product group by menuid)
但mysql不肯幹,you can't specify target table 'blur_article' for update in from clause
也就是說你不能邊查邊刪啊。後來想如果這樣,那我全查出來再刪唄。
用了如下方法,成功了。記錄一下。
create table tmp as select min(pid) as col1 from qb_product group by menuid;
delete from qb_product where pid not in (select col1 from tmp);
drop table tmp;
建立乙個臨時表,用完就刪除掉。
mysql刪除表中重複值
工作中遇到這麼個需求,有個 eh 表沒有唯一索引,當表中有一大堆資料後,又需要建個唯一索引,而這個表中還很多重複值,問我咋辦?一句話形容就是 表中重複值太多,如何去重。舉個白痴例子 1.建表a create table a id int,name char 10 2.插入資料 id name 1 a...
mysql刪除重複的記錄 MYSQL刪除重覆記錄
mysql刪除重覆記錄,儲存id最小的一條 方法1 1 建立乙個臨時表,選取需要的資料。2 清空原表。3 臨時表資料匯入到原表。4 刪除臨時表。mysql select from student id name 11 aa 12 aa 13 bb 14 bb 15 bb 16 cc 6 rows i...
MySQL刪除重複行
網上有些做法是這樣的 delete from table name where id not in select min id from select min id as min id from table name group by duplicate column b 發現這樣在本地跑是可行的,...