MYSQL刪除重復(fù)數(shù)據(jù)的簡單方法
CREATETABLE`users`(
`id`int(10)NOTNULLAUTO_INCREMENT,
`name`char(50)NOTNULL,
PRIMARYKEY(`id`)
)
deletefromuserswhereidin(selectmin(id)fromusersgroupbynamehavingcount(name)>1);
結(jié)果報錯:1093youcan'tspecifytargettable....
原因是mysql刪除動作不能帶有本表的查詢動作,意思是你刪除users表的東西不能以users表的信息為條件所以這個語句會報錯,執(zhí)行不了。只要通過創(chuàng)建臨時表作為查詢條件。如下
deletefromuserswhereidin(select*from(selectmin(id)fromusersgroupbynamehavingcount(name)>1));
還要注意deletefromusers這里不能用別名
其他方法。
deleteusersasafromusersasa,(selectmin(id)id,namefromusersgroupbynamehavingcount(name)>1
)asbwherea.name=b.nameanda.id<>b.id;
建立臨時表:
createtabletmp_usersselectmin(`id`),`name`fromusersgroupbyname;
truncatetableusers;
insertintousersselect*fromtmp_users;
droptabletmp_users;
- mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實例詳解
- Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù)
- MySQL中刪除重復(fù)數(shù)據(jù)的簡單方法
- 刪除MySQL重復(fù)數(shù)據(jù)的方法
- MySQL 刪除數(shù)據(jù)庫中重復(fù)數(shù)據(jù)方法小結(jié)
- MySQL數(shù)據(jù)庫中刪除重復(fù)記錄的方法總結(jié)[推薦]
- 刪除mysql數(shù)據(jù)庫中的重復(fù)數(shù)據(jù)記錄
- Mysql刪除重復(fù)數(shù)據(jù)保留最小的id 的解決方法
相關(guān)文章
MySql官方手冊學(xué)習(xí)筆記2 MySql的模糊查詢和正則表達式
MySQL提供標準的SQL模式匹配,以及擴展正則表達式模式匹配的格式2012-10-10MySQL創(chuàng)建唯一索引時報錯Duplicate?entry?*?for?key問題
這篇文章主要介紹了MySQL創(chuàng)建唯一索引時報錯Duplicate?entry?*?for?key問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09