1、在我们的电脑上打开数据库,这里新建一张含有重复数据的user表做示例。
2、我们输入“select * from user where name in (select name from user group by name having count(name) > 1) ”sql语句,点击运行可以看到查询出了数据库中user表的重复数据。
3、通过“delete from user where name in (select name from user group by name having count(name) > 1) ”sql语句删除姓名重复的数据。
4、通过“select distinct name from user”sql语句来去掉重复数据,这里去掉了张三的重复数据。
5、通过“select distinct class from user”sql语句来去掉班级相同的重复数据。
6、也可以通过“select distinct name,class from user”来去掉两个字段的重复数据。
创建测试表
create table t
(a varchar(10),
b int,
c varchar(1))
insert into t values ('张三',1,'Y')
insert into t values ('张三',2,'Y')
insert into t values ('张三',3,'N')
insert into t values ('李四',1,'Y')
insert into t values ('王五',1,'Y')
insert into t values ('王五',2,'Y')
执行查询
select a,MIN(b) b,c from t
where a in
(select a from t group by a having COUNT(distinct c)>1)
group by a,c
结果
select a,c from table group by a,c order by a,c
select * from table
group by C
having count(distinct(C))>1