国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql刪除字段重復的數據sql語句

2024-07-24 12:37:31
字體:
來源:轉載
供稿:網友

在mysql中刪除重復記錄我們有多種方法,下面我介紹利用臨時表與ALTER IGNORE TABLE命令來實現,希望些方法對各位會有所幫助.

mysql刪除字段重復的數據,經過搜索剛開始是這樣做的:

  1. delete from v_togo 
  2. where tel in (select tel from v_togo group by tel having count(tel) > 1) 
  3. and togoid not in (select min(togoid) from v_togo group by tel having count(tel )>1) 

結果mysql報錯:you can't specify target table 'v_togo' for update in from clause

然后我是這樣解決的,分三個步驟:

1、create table tmp as select max(togoid) as toid from v_togo group by tel;先把要處理的字段存儲到臨時表

2、delete from v_togo where togoid not in (select toid from tmp);根據臨時表進行篩選

3、drop table tmp;刪除臨時表

例,我是想做一個去重復操作,比如,如下表:

  1. id       title                  
  2.  
  3. 1           張三                 
  4.  
  5. 2           李四                  
  6.  
  7. 3           張三                  
  8.  
  9. 4           王五                  
  10.  
  11. 5           李四 

最終結果是:

  1. id       title                  
  2.  
  3. 1         張三                  
  4.  
  5. 2         李四                  
  6.  
  7. 4         王五 

替換方案,代碼如下:

  1. create table tmp as select min(id) as col1 from blur_article group by title;delete from blur_article where id not in (select col1 from tmp); drop table tmp;  --Vevb.com 

已經測試,盡請使用這樣就ok了,

或者這樣操作,代碼如下:

ALTER IGNORE TABLE `表名` ADD UNIQUE (`唯一索引字段名`);

刪除重復的數據,只保留一條.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻栗坡县| 呼伦贝尔市| 阿拉善盟| 乌拉特后旗| 剑阁县| 微山县| 达拉特旗| 西吉县| 于都县| 信宜市| 定边县| 定日县| 平利县| 浠水县| 黔江区| 尼玛县| 成都市| 余姚市| 深州市| 洛扎县| 太仆寺旗| 凤山县| 共和县| 枞阳县| 深圳市| 新龙县| 巫山县| 蓬安县| 彭州市| 东兰县| 许昌市| 永平县| 慈利县| 石狮市| 二手房| 会东县| 华蓥市| 宜州市| 日喀则市| 偏关县| 涞源县|