mysql消除重复行的关键字_MySQL 消除重复行的一些方法
/*
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
/*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最基本的增删改查语句.在进行"增删改查"的操作之前,先建立一个包含数据表student的数据库,新建表grade(具体操作可以见上一篇). 一."增"-添加 ...
- mysql重复你数据标识_MySQL 处理重复数据
有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 下面将为大家介绍如何防止数据表出现重复数据及如何删除数据表中的重复数据. 防止表中 ...
- MySQL查询行记录关键字_MySQL数据库~~~~~查询行(文件的内容)
1. 单表查询 语法 select distinct 字段 from 库名.表名 where 条件 group by 字段 # 分组 having 筛选 # 过滤 order by 字段 # 排序 l ...
- mysql 查询出现次数最多的_MySQL查询重复出现次数最多的记录
MySQL查询的方法很多,下面为您介绍的MySQL查询语句用于实现查询重复出现次数最多的记录,对于学习MySQL查询有很好的帮助作用. SELECT keyword, count( * ) AS co ...
- mysql数据库增删改查关键字_MySQL数据库(增删改查语句)
MySQL数据库(增删改查语句) 一.登录数据库:----> mysql -uroot -proot;(对应用户名和密码) 二.SQL语句: 数据定义语言DDL 用来定义数据库.表.列,关键 ...
- 在mysql中怎样设置值唯一_mysql怎么设置行值唯一?
mysql可通过给行(字段)添加唯一性约束来设置行值唯一,语法格式"CREATE TABLE 表名 (字段名 数据类型 UNIQUE);"和"ALTER TABLE 表名 ...
- mysql数据库增删改查关键字_Mysql数据库,增删改查笔记(非常重要)-阿里云开发者社区...
新增数据: INSERT 语法: INSERT [INTO] [列名] VALUES; INSETR INTO 表名(列名1,列名2) VALUES(值1,值2); 如果INSETR语句中不写列名,则 ...
- linux mysql 命令行查询 乱码_mysql命令提示行连接乱码的解决
解决MySQL命令行无法连接问题错误ERROR1045(28000) 问题现象: 用navicat可以连接数据库 通过命令行mysql -uroot -p 输入密码后提示"ERROR 104 ...
- mysql设置行值唯一_mysql怎么设置行值唯一?
mysql可通过给行(字段)添加唯一性约束来设置行值唯一,语法格式"CREATE TABLE 表名 (字段名 数据类型 UNIQUE);"和"ALTER TABLE 表名 ...
最新文章
- Python 出现 can't use a string pattern on a bytes-like object
- 机器学习101:我们天天都在说的机器学习,究竟该怎么入门?
- 关于ark取得进程的镜像文件路径
- telegram电脑一直显示连接中_小事不求人!学会这个方法,一个电脑连接多个显示器不再是问题...
- Cocos2d JS 之消灭星星(九) 处理星星类之——移动和消灭星星
- C++中 Vector使用方法
- c# 创建委托 消息订阅_C#面向对象之委托和事件
- mogonvue账户密码存储的位置
- NYOJ975 - 关于521
- MVVM最佳解读和实践
- 给物体赋值 只改变transform.position的某一维,访问另一个脚本的变量以及给物体用脚本动态添加属性
- 程序员那些你不知道的事:高收入程序员年薪高于50万,近四成程序员单身
- 各种滤镜算法C语言,Photoshop入门学习之PS 滤镜算法原理——染色玻璃
- 金铖计算机学校,金铖计算机学校附近好玩的
- MT5和MT4交易软件有什么区别?
- IPD需求和市场管理流程
- SQLserver数据字段详情
- 问责制Accountability
- 全面揭秘Windows激活原理
- 倍加福倾角传感器INX360D-F99-I2E2-V15