创建测试数据
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