国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

MySQL消除重復行方法步驟

來源:懂視網 責編:小采 時間:2020-11-09 08:44:21
文檔

MySQL消除重復行方法步驟

MySQL消除重復行方法步驟:本文主要介紹了MySQL 消除重復行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/* MySQL 消除重復行的一些方法 ---Chu Minfei ---2010-08-12 22:49:44.660 --引用轉載請注明出處:http://blog.csdn.NET/feixianx
推薦度:
導讀MySQL消除重復行方法步驟:本文主要介紹了MySQL 消除重復行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/* MySQL 消除重復行的一些方法 ---Chu Minfei ---2010-08-12 22:49:44.660 --引用轉載請注明出處:http://blog.csdn.NET/feixianx

本文主要介紹了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

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MySQL消除重復行方法步驟

MySQL消除重復行方法步驟:本文主要介紹了MySQL 消除重復行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/* MySQL 消除重復行的一些方法 ---Chu Minfei ---2010-08-12 22:49:44.660 --引用轉載請注明出處:http://blog.csdn.NET/feixianx
推薦度:
標簽: 步驟 方法 重復
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 一级毛片免费网站 | 黄色网站在线观看视频 | 国内精品1区1区3区4区 | 日韩在线不卡 | 欧洲综合网 | 永久在线毛片免费观看 | 亚洲日韩精品欧美一区二区 | 97在线资源| 日本我不卡 | 日韩视频免费 | 国产精品成人久久久久久久 | 国产精品久久二区三区色裕 | 欧美日韩国产一区 | 国产欧美日韩另类 | 亚洲综合精品一区二区三区中文 | 久久久久久亚洲精品中文字幕 | 91精品国产色综合久久不卡蜜 | 精品一区二区三区在线播放 | 国产在线一区视频 | 精品一区二区三区免费视频 | xxxx性xx另类| 综合毛片| 国产精品一区二区三区四区五区 | 欧美区一区 | 亚洲午夜伦理 | 国产91精品一区二区麻豆亚洲 | 亚洲综合婷婷 | 国产成人精视频在线观看免费 | 精品一区视频 | 国产欧美日韩在线视频 | 最新国产网址 | 国产在线精品成人一区二区三区 | 亚洲国产激情一区二区三区 | 久久99精品国产麻豆宅宅 | 免费国产最新进精品视频 | 国产成人高清一区二区私人 | 日韩国产免费一区二区三区 | 久久91精品国产一区二区 | 亚洲欧美日韩网站 | 97r久久精品国产99国产精 | 国产精品99久久久久久www |