我的表里面有 id, xjbh, xh 等字段,我现在发现了大量的 xjbh和xh重复的,例如:
id xjbh xh
1 10001 1
2 10001 2
3 10001 1
4 10001 3
5 10002 1
6 10002 2
7 10002 1
8 10003 1
9 10003 2
10 10003 1如何找到 id xjbh xh
1 10001 1
3 10001 1
5 10002 1
7 10002 1
8 10003 1
10 10003 1其实我的表里xjbh 是可以重复的,但是相同的xjbh的xh是不能重复,所以要找出相同xjbh的重复xh,如何查找,请指教
id xjbh xh
1 10001 1
2 10001 2
3 10001 1
4 10001 3
5 10002 1
6 10002 2
7 10002 1
8 10003 1
9 10003 2
10 10003 1如何找到 id xjbh xh
1 10001 1
3 10001 1
5 10002 1
7 10002 1
8 10003 1
10 10003 1其实我的表里xjbh 是可以重复的,但是相同的xjbh的xh是不能重复,所以要找出相同xjbh的重复xh,如何查找,请指教
解决方案 »
- 用MFC ODBC访问数据库时的问题
- 我想问一个关于sql server2000记录图片信息的问题
- 驱动程序的 SQLAllocHandle on SQL_HANDLE_ENV 失败
- 联合查询结果重复出现的问题
- 谁能帮我解决sql触发器的问题
- 大家好我有个入门问题
- 我想装SQL Server,但我是XP系统,只能装个人版或者开发版,谁能告诉我有什么区别啊,哪个好啊
- 如何对uniqueifier 列进行分区
- 如何使用SQL SERVER2000导出HTML文件到另一台服务器的共享目录?
- 大家能告诉我一本关于SQL SERVER7.0的好书吗?
- 问一句update语句
- 问如何将table1改为一个变量
FROM TB T
WHERE (SELECT COUNT(1) FROM TB WHERE XJBH=T.XJBH AND XH=T.XH)>1
declare @tb table([id] int,[xjbh] int,[xh] int)
insert @tb
select 1,10001,1 union all
select 2,10001,2 union all
select 3,10001,1 union all
select 4,10001,3 union all
select 5,10002,1 union all
select 6,10002,2 union all
select 7,10002,1 union all
select 8,10003,1 union all
select 9,10003,2 union all
select 10,10003,1select * from @tb a where (select count(*) from @tb where xjbh=a.xjbh and xh=a.xh)>1
/*
id xjbh xh
----------- ----------- -----------
1 10001 1
3 10001 1
5 10002 1
7 10002 1
8 10003 1
10 10003 1(6 行受影响)*/
if object_id('[TB]') is not null drop table [TB]
go
create table [TB]([id] int,[xjbh] int,[xh] int)
insert [TB]
select 1,10001,1 union all
select 2,10001,2 union all
select 3,10001,1 union all
select 4,10001,3 union all
select 5,10002,1 union all
select 6,10002,2 union all
select 7,10002,1 union all
select 8,10003,1 union all
select 9,10003,2 union all
select 10,10003,1
---查询---
SELECT *
FROM TB T
WHERE (SELECT COUNT(1) FROM TB WHERE XJBH=T.XJBH AND XH=T.XH)>1---结果---
id xjbh xh
----------- ----------- -----------
1 10001 1
3 10001 1
5 10002 1
7 10002 1
8 10003 1
10 10003 1(所影响的行数为 6 行)
from ta a
where exists(select 1 from ta where xjbh = a.xjbh and xh = 1 and id != a.id)
and xh = 1