六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

MySQL消除重復行方法分享

[摘要]本文主要介紹了MySQL 消除重復行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/*MySQL 消除重復行的一些方法---Chu Minfei---2010-08-12 22:49...
本文主要介紹了MySQL 消除重復行的一些方法,需要的朋友可以參考下,希望能幫助到大家。

sql語句


/*
MySQL 消除重復行的一些方法
---Chu Minfei
---2010-08-12 22:49:44.660
--引用轉載請注明出處:http://blog.csdn.NET/feixianxxx
*/
----------------全部字段重復------------------------
 --1使用表替換來刪除重復項
 create table test_1(id int,value int);
 insert test_1 select 1,2 union all select 1,2 union all select 2,3;
 --建立一個和源表結構一樣的空的臨時表
 create table tmp like test_1;
 --向臨時表插入不重復的記錄
 insert tmp select distinct * from test_1;
 --刪除原表
 drop table test_1;
 --更改臨時表名為目標表
 rename table tmp to test_1;
 --顯示
 mysql> select * from test_1;
+------+-------+
  id    value  
+------+-------+
   1     2  
   2     3  
+------+-------+
 --2.添加auto_increment屬性列(這個方法只能用于MyISAM或者BDB引擎的表)
 create table test_1(id int,value int) engine=MyISAM;
 insert test_1 select 1,2 union all select 1,2 union all select 2,3;
 alter table test_1 add id2 int not null auto_increment,
 add primary key(id,value,id2);
 select * from test_1;
+----+-------+-----+
  id   value   id2  
+----+-------+-----+
  1     2    1  
  1     2    2  
  2     3    1  
+----+-------+-----+
  delete from test_1 where id2<>1;
  alter table test_1 drop id2;
  select * from test_1;
  +----+-------+
  id   value  
+----+-------+
  1     2  
  2     3  
+----+-------+
-------------------部分字段重復---------------------
--1.加索引的方式
 create table test_2(id int,value int);
 insert test_2 select 1,2 union all select 1,3 union all select 2,3;
 Alter IGNORE table test_2 add primary key(id);
 select * from test_2;
 +----+-------+
  id   value  
+----+-------+
  1     2  
  2     3  
+----+-------+
 我們可以看到 1 3 這條記錄消失了 
 我們這里也可以使用Unique約束 因為有可能列中有NULL值,但是這里NULL就可以多個了..
 --2.聯合表刪除
 create table test_2(id int,value int);
 insert test_2 select 1,2 union all select 1,3 union all select 2,3;
 delete A from test_2 a join (select MAX(value) as v ,ID from test_2 group by id) b
 on a.id=b.id and a.value<>b.v;
 select * from test_2;
 +------+-------+
  id    value  
+------+-------+
   1     3  
   2     3  
+------+-------+
--3.使用Increment_auto也可以就是上面全部字段去重的第二個方法
--4.容易錯誤的方法
--有些朋友可能會想到子查詢的方法,我們來試驗一下
 create table test_2(id int,value int);
 insert test_2 select 1,2 union all select 1,3 union all select 2,3;
 delete a from test_2 a where exists(select * from test_2 where a.id=id and a.value<value);
 /*ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause*/
 
 目前,您不能從一個表中刪除,同時又在子查詢中從同一個表中選擇。
 
 
 ------------------刪除特定重復行--------------
 --主要通過order by +limit 或者直接limit 
 create table test_3(id int,value int);
 insert test_3 select 1,2 union all select 1,3 union all select 1,4 union all select 2,3;
 --這是要保留ID=1 value最小的那個記錄,刪除其他id為的記錄
 delete from test_3 where id=1 order by value desc limit 2;
 select * from test_3;
+------+-------+
  id    value  
+------+-------+
   1     2  
   2     3  
+------+-------+
 如果你只想刪除任意的記錄 保留一條 就可以去掉order by

相關推薦:

關于消除重復行的詳細介紹

消除數組重復項,并且讓鍵值重新從0開始排列

HTML 外邊距折疊,以及消除的辦法_html/css_WEB-ITnose

以上就是MySQL消除重復行方法分享的詳細內容,更多請關注php中文網其它相關文章!


學習教程快速掌握從入門到精通的SQL知識。




主站蜘蛛池模板: 欧美综合激情 | 三级特黄高清完整视频 | 色呦呦在线免费观看 | 天天做天天爱天天干 | 亚洲天堂男人在线 | 欧美囗交 | 日日摸日日添日日透 | 深夜福利免费视频 | 青青草原免费在线观看视频 | 一级久久| 亚洲成人福利在线 | 色综久久 | 日韩毛片在线免费观看 | 色婷婷一区二区三区四区成人网 | 日韩免费一区二区三区 | 欧美性色黄在线视频 | 欧美视频在线第一页 | 伊人| 亚洲五月六月丁香激情 | 在线观看国产黄色 | 中文字幕人成乱在线视频 | 视频在线一区二区 | 婷婷中文 | 一区二区在线欧美日韩中文 | 偷窥自拍亚洲色图 | 一二三四在线观看高清在线观看一 | 欧美无人区码卡二三卡四卡 | 日韩中文字幕视频 | 伊人精品久久久大香线蕉99 | 视频一区二区中文字幕 | 欧美一卡二卡3卡4卡无卡六卡七卡科普 | 日本国产一区在线观看 | 丝袜美腿亚洲一区二区图片 | 日本黄 色 成 年 人免费观看 | 色综合久久丁香婷婷 | 日日噜噜夜夜狠狠久久丁香 | 欧美一区二区三区日韩免费播 | 自拍一区在线观看 | 四虎影院地址 | 日本在线观看网址 | 偷偷操不一样的久久 |