最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL 消除重復(fù)行的一些方法

 更新時間:2017年05月20日 13:55:53   投稿:mdxy-dxy  
這篇文章主要介紹了MySQL 消除重復(fù)行的一些方法,需要的朋友可以參考下

sql語句

/*
MySQL 消除重復(fù)行的一些方法
---Chu Minfei
---2010-08-12 22:49:44.660
--引用轉(zhuǎn)載請注明出處:http://blog.csdn.NET/feixianxxx
*/
----------------全部字段重復(fù)------------------------
 --1使用表替換來刪除重復(fù)項
 create table test_1(id int,value int);
 insert test_1 select 1,2 union all select 1,2 union all select 2,3;
 --建立一個和源表結(jié)構(gòu)一樣的空的臨時表
 create table tmp like test_1;
 --向臨時表插入不重復(fù)的記錄
 insert tmp select distinct * from test_1;
 --刪除原表
 drop table test_1;
 --更改臨時表名為目標(biāo)表
 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 |
+----+-------+
-------------------部分字段重復(fù)---------------------
--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.聯(lián)合表刪除
 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*/
 
 目前,您不能從一個表中刪除,同時又在子查詢中從同一個表中選擇。
 
 
 ------------------刪除特定重復(fù)行--------------
 --主要通過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 

相關(guān)文章

  • MySQL 的模塊不能安裝的解決方法

    MySQL 的模塊不能安裝的解決方法

    這篇文章主要介紹了MySQL 的模塊不能安裝的解決方法的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • MySql 5.6.14 Win32位免安裝解壓縮版配置教程

    MySql 5.6.14 Win32位免安裝解壓縮版配置教程

    本文給大家介紹mysql 5.6.14 win32 位免安裝解壓縮版配置方法,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,對mysql5.6.14 免安裝解壓縮版配置方法感興趣的朋友一起看看吧
    2016-11-11
  • MySQL授予用戶權(quán)限命令詳解

    MySQL授予用戶權(quán)限命令詳解

    這篇文章主要給大家介紹了關(guān)于MySQL授予用戶權(quán)限命令的相關(guān)資料,授權(quán)就是為某個用戶賦予某些權(quán)限,例如可以為新建的用戶賦予查詢所有數(shù)據(jù)庫和表的權(quán)限,需要的朋友可以參考下
    2023-11-11
  • Linux下卸載MySQL數(shù)據(jù)庫

    Linux下卸載MySQL數(shù)據(jù)庫

    如何在Linux平臺卸載MySQL呢?這篇文章主要介紹了Linux下卸載MySQL數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 理解MySQL——索引與優(yōu)化總結(jié)

    理解MySQL——索引與優(yōu)化總結(jié)

    本篇文章主要介紹了MySQL——索引與優(yōu)化,索引對查詢的速度有著至關(guān)重要的影響,理解索引也是進行數(shù)據(jù)庫性能調(diào)優(yōu)的起點。有興趣的可以了解一下。
    2016-12-12
  • MySQL的常用命令集錦

    MySQL的常用命令集錦

    這篇文章主要介紹了MySQL的常用命令集錦,堪稱初學(xué)者需要掌握的MySQL命令大全,其中系統(tǒng)命令行環(huán)境是基于類Unix系統(tǒng)來作例子的,需要的朋友可以參考下
    2015-11-11
  • Linux下二進制方式安裝mysql5.7版本和系統(tǒng)優(yōu)化的步驟

    Linux下二進制方式安裝mysql5.7版本和系統(tǒng)優(yōu)化的步驟

    這篇文章主要介紹了Linux下二進制方式安裝mysql5.7版本和系統(tǒng)優(yōu)化的步驟,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • MySQL如何為字段添加默認時間淺析

    MySQL如何為字段添加默認時間淺析

    這篇文章主要給大家介紹了關(guān)于MySQL如何為字段添加默認時間的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • mysql 將字段time按天/月/年分組

    mysql 將字段time按天/月/年分組

    表中有一個字段time,格式為Unix時間戳,現(xiàn)需要按照該字段統(tǒng)計每天的記錄數(shù)并按天/月/年分組
    2014-07-07
  • MySQL表和列的注釋總結(jié)

    MySQL表和列的注釋總結(jié)

    在本篇文章里小編給大家分享了關(guān)于MySQL表和列的注釋相關(guān)知識點內(nèi)容總結(jié),需要的朋友們學(xué)習(xí)下。
    2019-05-05

最新評論

宁明县| 河南省| 汝阳县| 孟连| 旅游| 林周县| 古交市| 水富县| 洛浦县| 怀柔区| 嘉祥县| 四会市| 龙井市| 页游| 廊坊市| 兰考县| 乡城县| 沙田区| 红桥区| 中卫市| 虹口区| 新田县| 平果县| 平顶山市| 南昌县| 阳春市| 聂拉木县| 东莞市| 邵阳县| 胶南市| 太仓市| 台东市| 基隆市| 施秉县| 南康市| 通辽市| 雅安市| 巴林右旗| 定结县| 抚松县| 襄城县|