SQL查找某一字段相同,某一字段不同的数据
转载
创建测试表
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
结果