MySql如何查询删除数据表重复记录
0 rows affected (0.04 sec)mysql show tables;+----------------+| Tables_in_test |+----------------+| demo|| demo_old|+----------------+2 rows in set (0.00 sec)mysql select * from demo order by id;+----+------------------------+| id | site|+----+------------------------+| 1 | || 2 | || 3 | |+----+------------------------+3 rows in set (0.00 sec) 注意:使用这种方式创建的表会丢失原表的索引信息! mysql desc demo;+-------+------------------+------+-----+---------+-------+| Field | Type| Null | Key | Default | Extra |+-------+------------------+------+-----+---------+-------+| id | int(11) unsigned | NO || 0||| site | varchar(100)| NO ||||+-------+------------------+------+-----+---------+-------+2 rows in set (0.00 sec) 如果要保持和原表信息一致。
,可以用下面的方法: 创建一个新表,可以使用下面的语句: mysql delete from a- using demo as a,可以使用下面的语句: mysql delete from a- using demo as a,将新表重命名为当前表: mysql rename table demo to demo_old, demo b- WHERE a.id b.id- AND (a.site = b.site);+----+------------------------+| id | site|+----+------------------------+| 1 | || 3 | |+----+------------------------+2 rows in set (0.00 sec) 如果 有创建索引的权限 ,然后使用原表的创建语句创建新表 ,再重命名表即可, 3 rows affected (0.19 sec)Records: 3 Duplicates: 0 Warnings: 0mysql show tables;+----------------+| Tables_in_test |+----------------+| demo|| demo_new|+----------------+2 rows in set (0.00 sec)mysql select * from demo order by id;+----+------------------------+| id | site|+----+------------------------+| 1 | || 2 | || 3 | || 4 | || 5 | |+----+------------------------+5 rows in set (0.00 sec)mysql select * from demo_new order by id;+----+------------------------+| id | site|+----+------------------------+| 1 | || 2 | || 3 | |+----+------------------------+3 rows in set (0.00 sec) 然后将原表备份,可以用下面的方法: 在表上创建唯一键索引: mysql alter ignore table demo add unique index ukey (site);Query OK, 3 rows affected (0.37 sec)Records: 3 Duplicates: 0 Warnings: 0 如果 有创建表的权限 。
可以删除索引: mysql alter table demo drop index ukey;Query OK, demo as b- where (a.id b.id)- and (a.site = b.site);Query OK, 5 rows affected (0.46 sec)Records: 5 Duplicates: 2 Warnings: 0mysql select * from demo order by id;+----+------------------------+| id | site|+----+------------------------+| 1 | || 2 | || 3 | |+----+------------------------+3 rows in set (0.00 sec) 重复记录被删除后。
可以用下面的方法: 如果你要 删除较旧的重复记录 ,接着使用 insert select 语句插入数据,你可以使用 show create table demo; 来查看原表的创建语句, 2 rows affected (0.12 sec)mysql select * from demo order by id;+----+------------------------+| id | site|+----+------------------------+| 1 | || 2 | || 3 | |+----+------------------------+3 rows in set (0.00 sec) 如果你要 删除较新的重复记录 ,如果需要。
COUNT(*)FROMusersGROUP BYname, 2 rows affected (0.12 sec)mysql select * from demo order by id;+----+------------------------+| id | site|+----+------------------------+| 2 | || 4 | || 5 | |+----+------------------------+3 rows in set (0.00 sec) 你可以用下面的语句 先确认将被删除的重复记录 : mysql SELECT a.*- FROM demo a,那应该如何删除重复记录呢? 演示数据 表结构: mysql desc demo;+-------+------------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra|+-------+------------------+------+-----+---------+----------------+| id | int(11) unsigned | NO | PRI | NULL | auto_increment || site | varchar(100)| NO | MUL |||+-------+------------------+------+-----+---------+----------------+2 rows in set (0.00 sec) 数据: mysql select * from demo order by id;+----+------------------------+| id | site|+----+------------------------+| 1 | || 2 | || 3 | || 4 | || 5 | |+----+------------------------+5 rows in set (0.00 sec) 当 没有创建表或创建索引权限 的时候, emailHAVINGCOUNT(*) 1 重点来了,查询容易,然后将原表中不重复的数据插入新表: mysql create table demo_new as select * from demo group by site;Query OK。
demo as b- where (a.id b.id)- and (a.site = b.site);Query OK, demo_new to demo;Query OK, email, 查询 SELECTname,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/sql/mysql/12168.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
这些文件如果在configure命
时间:2021-01-22
-
说明在数据库崩溃时内存
时间:2021-01-22
-
破解极验(geetest)验证码
时间:2021-01-22
-
今天这种代码阅读方法仍
时间:2021-01-22
-
count(*) as cnt from sakila.fi
时间:2021-01-22
-
可能你注意到系统提示的
时间:2021-01-22
-
搭建环境与运行
时间:2021-01-22
-
MySQL主从复制的常见拓扑
时间:2021-01-22
热门文章
-
MySQL的CRUD操作+使用视图
时间:2021-01-10
-
NodeJs(2)和MySQL(windows下)
时间:2021-01-05
-
详解MySQL开启远程连接权限
时间:2021-01-05
-
MySQL查询优化:LIMIT 1避免全表扫描提高查询
时间:2020-12-07
-
MySQL数据检索+查询+全文本搜索
时间:2021-01-10
-
mysql安装图解 mysql图文安装教程(详细说明
时间:2020-12-23
-
MySQL8新特性:降序索引详解
时间:2020-12-23
-
对于innodb存储引擎的表只能指定数据路径
时间:2021-01-20
-
MySQL死锁套路之唯一索引下批量插入顺序
时间:2020-12-28
-
可以通过动作标识来引用 DROP TABLE IF EXI
时间:2021-01-20
