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ù)并只保留一條實(shí)例詳解
- 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的模糊查詢和正則表達(dá)式
MySQL提供標(biāo)準(zhǔn)的SQL模式匹配,以及擴(kuò)展正則表達(dá)式模式匹配的格式2012-10-10詳細(xì)深入聊一聊Mysql中的int(1)和int(11)
mysql數(shù)據(jù)庫作為當(dāng)前常用的關(guān)系型數(shù)據(jù)庫,肯定會遇到設(shè)計表的需求,下面對設(shè)計表時int類型的設(shè)置進(jìn)行分析,下面這篇文章主要給大家介紹了關(guān)于Mysql中int(1)和int(11)的相關(guān)資料,需要的朋友可以參考下2022-08-08MySQL創(chuàng)建唯一索引時報錯Duplicate?entry?*?for?key問題
這篇文章主要介紹了MySQL創(chuàng)建唯一索引時報錯Duplicate?entry?*?for?key問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09