首页 > 数据库 >mysql 查找删除重复数据

mysql 查找删除重复数据

时间:2023-03-20 17:47:48浏览次数:40  
标签:qq 删除 people MySQL 查找 mysql test com email

创建测试数据

MySQL [test]> create table people (id int auto_increment primary key,name varchar(50) not null ,email varchar(100) not null);
Query OK, 0 rows affected (0.02 sec)

MySQL [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| people         |
| tb_emp1        |
+----------------+
2 rows in set (0.00 sec)

MySQL [test]> insert into people (name,email) values ('张三',"[email protected]");
Query OK, 1 row affected (0.00 sec)

MySQL [test]> insert into people (name,email) values ('张三',"[email protected]");
Query OK, 1 row affected (0.00 sec)

MySQL [test]> insert into people (name,email) values ('李三',"[email protected]");
Query OK, 1 row affected (0.00 sec)

MySQL [test]> insert into people (name,email) values ('章三',"[email protected]");
Query OK, 1 row affected (0.01 sec)

MySQL [test]> select * from people;
+----+--------+-----------------+
| id | name   | email           |
+----+--------+-----------------+
|  1 | 张三   | [email protected] |
|  2 | 张三   | [email protected] |
|  3 | 李三   | [email protected]    |
|  4 | 章三   | [email protected]    |
+----+--------+-----------------+
4 rows in set (0.00 sec)

MySQL [test]>

比如查询email 字段重复的数据

MySQL [test]> select email,count(*) from people group by email;
+-----------------+----------+
| email           | count(*) |
+-----------------+----------+
| [email protected]    |        2 |
| [email protected] |        2 |
+-----------------+----------+
2 rows in set (0.00 sec)

查出哪些email重复

MySQL [test]> select email from people group by email having count(*)>1;
+-----------------+
| email           |
+-----------------+
| [email protected]    |
| [email protected] |
+-----------------+
2 rows in set (0.00 sec)

 删除重复数据,使用 DELETE JOIN 语句,

MySQL [test]> select * from people;
+----+--------+--------------------+
| id | name   | email              |
+----+--------+--------------------+
|  1 | 张三   | [email protected]    |
|  2 | 张三   | [email protected]    |
|  3 | 李三   | [email protected]       |
|  4 | 章三   | [email protected]       |
|  5 | 张7    | [email protected]      |
|  6 | 87     | [email protected]   |
|  7 | 87sd   | [email protected] |
+----+--------+--------------------+
7 rows in set (0.01 sec)

MySQL [test]> delete s1 from people as s1 inner join people as s2 where s1.id<s2.id and s1.email = s2.email;
Query OK, 2 rows affected (0.01 sec)

MySQL [test]> select * from people;
+----+--------+--------------------+
| id | name   | email              |
+----+--------+--------------------+
|  2 | 张三   | [email protected]    |
|  4 | 章三   | [email protected]       |
|  5 | 张7    | [email protected]      |
|  6 | 87     | [email protected]   |
|  7 | 87sd   | [email protected] |
+----+--------+--------------------+
5 rows in set (0.00 sec)

 

标签:qq,删除,people,MySQL,查找,mysql,test,com,email
From: https://www.cnblogs.com/libruce/p/17237108.html

相关文章

  • 8.1.6mysql的条件子查询
    如题。一个不算复杂的东西,书上说的太绕了,不能忍。准备数据,这里有三张表(偷懒,各种编号都是整数)。t1是学生基本情况表,重要的字段是学号和姓名。t2是课程表,包括课程号和课程......
  • install mysql in docker
    installmysqlindocker#step-1:configuremysql_port='3306'mysql_password='123qwe'mysql_data_dir="~/docker/mysqld_${mysql_port}/data"#step-2:delete-da......
  • mysql视图
    1.常见的数据库对象2.视图2.1为什么使用视图视图一方面可以帮我们使用表的一部分而不是所有的表,另一方面也可以针对不同的用户制定不同的查询视图。比如,针对一个公司的......
  • mysql-根据身份证号更新性别
    根据身份证号查询更新性别:update uufs_ltx_info set xb= (case SUBSTRING(sfzh,17,1)&1 when 1 then '男' else '女' end);  根据身份证号查询更新出生......
  • MySQL数据库的安装(保姆教学)
    MySQL数据库的安装(如果安装失败请看链接https://www.cnblogs.com/seeyouone/p/17236660.html)注意:必须用系统管理员身份运行mysql安装程序。安装目录切记不要用中文。......
  • MySQL Incorrect string value: '\xF0\x9F\x8D\x8A' for column 'column_name' at
    将"XXXXXX......
  • MySQL自增ID的知识点总结
    MySQL自增ID的知识点总结1.使用自增ID的优缺点优点主键页以近乎顺序的方式填写,提升了页的利用率索引更加紧凑,性能更好查询时数据访问更快节省空间连续增长的值......
  • MySQL数据库的卸载(保姆教学)
     步骤一:软件的卸载准备学习网络编程时,TCP/IP协议程序有服务器端和客户端。mysql这个数据库管理软件是使用TCP/IP协议。我们现在要卸载的是mysql的服务器端,它没有界面。......
  • windows 重启mysql服务
    方式一:命令窗口以管理员身份打开命令窗口(cmd) 关闭mysql服务 netstopmysql  启动mysql服务 netstartmysql  方式二:在服务中重启mysql服务  找......
  • Node向Mysql数据发送请求响应请求
    //导入mysql依赖(想要先安装依赖,在终端输入:npminstallmysql)varmysql=require('mysql');//配置mysql的数据信息varmy=mysql.createConnection({//mysql连接地址......