婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > MySQL 消除重復行的一些方法

MySQL 消除重復行的一些方法

熱門標簽:昆明智能外呼系統中心 手機用地圖標注工具 南宋地圖標注黃河華山 智能電銷機器人靠譜么 電銷機器人公眾號推送 長安區違法建房地圖標注 地圖標注培訓 電銷機器人說明書 安國在哪里辦理400電話

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.valuevalue);
 /*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 

您可能感興趣的文章:
  • mysql刪除重復行的實現方法
  • Mysql數據庫支持的存儲引擎對比
  • MySQL帶你秒懂索引下推
  • MySQL 如何查找刪除重復行

標簽:合肥 武漢 長沙 南昌 吉安 東莞 潛江 江門

巨人網絡通訊聲明:本文標題《MySQL 消除重復行的一些方法》,本文關鍵詞  MySQL,消除,重復,行的,一些,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《MySQL 消除重復行的一些方法》相關的同類信息!
  • 本頁收集關于MySQL 消除重復行的一些方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 苗栗市| 芦山县| 朝阳市| 安国市| 福贡县| 泊头市| 周宁县| 且末县| 开鲁县| 罗甸县| 晋江市| 安庆市| 德兴市| 阜康市| 沁水县| 德惠市| 福泉市| 张掖市| 长寿区| 厦门市| 武平县| 永城市| 九江县| 榆社县| 江口县| 新田县| 富锦市| 北辰区| 项城市| 台北县| 兴仁县| 邵武市| 弥渡县| 龙江县| 平阴县| 赣州市| 开平市| 华容县| 宜兴市| 霍邱县| 抚顺市|