select a.* from tb a join tb b on a.id=b.id and a.name<>b.name
select * from tb where c1 in (select c1 from tb group by c1 having count(1) > 1)
/* 如何查找出所有一個字段相同, 另一個字段不同的記錄(同一個table), 高手請指點一下如何寫SQL語句,感謝!!! */--生成测试数据: go if OBJECT_ID('tbl')is not null drop table tbl go create table tbl( ID INT, COL VARCHAR(2), ) go insert tbl select 1,'A' union all select 1,'B' union all select 1,'A' union all select 2,'A' union all select 2,'C' union all select 3,'A' union all select 3,'A' union all select 3,'A'--以上数据中的ID为1,2的满足楼主的要求: select *from tbl where ID in (select ID from tbl group by ID having COUNT(distinct COL)>1) --或者: select *from tbl where ID in (select distinct C.num from (select a.ID as num,a.COL from tbl a inner join tbl b on a.ID=b.ID and a.COL<>b.COL)c) /* ID COL 1 A 1 B 1 A 2 A 2 C */
select a.* from tb a join tb b on a.id=b.id and a.name<>b.name
(select c1 from tb group by c1 having count(1) > 1)
如何查找出所有一個字段相同,
另一個字段不同的記錄(同一個table),
高手請指點一下如何寫SQL語句,感謝!!!
*/--生成测试数据:
go
if OBJECT_ID('tbl')is not null
drop table tbl
go
create table tbl(
ID INT,
COL VARCHAR(2),
)
go
insert tbl
select 1,'A' union all
select 1,'B' union all
select 1,'A' union all
select 2,'A' union all
select 2,'C' union all
select 3,'A' union all
select 3,'A' union all
select 3,'A'--以上数据中的ID为1,2的满足楼主的要求:
select *from tbl
where ID in
(select ID from tbl group by ID having COUNT(distinct COL)>1)
--或者:
select *from tbl
where ID in
(select distinct C.num from
(select a.ID as num,a.COL from tbl a inner join tbl b on a.ID=b.ID and a.COL<>b.COL)c)
/*
ID COL
1 A
1 B
1 A
2 A
2 C
*/