问一条查询语句

Magicwords 2008-11-11 04:40:53
表结构如下

序号 类型 编号
---------------------------------
1 A 10
2 A 10
3 A 11
4 B 10
4 B 11

现在我的查询是同类型的编号不能相同
1:查询出表中类型相同,编号相同的记录(不合格的记录)

2:查询出表中类型相同,但编号不相同的记录(合格的记录)


请达人们指点下,在线等,
...全文
82 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2008-11-11
  • 打赏
  • 举报
回复
--查询出表中类型相同,但编号不相同的记录(合格的记录) 
select * from #EE EE where Exists (select * from #EE where ttype=EE.ttype and Num<>EE.Num)
ws_hgo 2008-11-11
  • 打赏
  • 举报
回复
create table #EE
(
id int,
ttype nvarchar(20),
Num int
)
insert into #EE select 1,'A',10
union all select 2,'A',10
union all select 3,'A',11
union all select 4,'B',10
union all select 4,'B',11

--1:查询出表中类型相同,编号相同的记录(不合格的记录)
select * from #EE EE where Exists (select * from #EE where ttype=EE.ttype and Num=EE.Num and id<>EE.id)
marco08 2008-11-11
  • 打赏
  • 举报
回复

2:查询出表中类型相同,但编号不相同的记录(合格的记录) ??

2 A 10
3 A 11


1 A 10
3 A 11

这样的组合符合要求吗
tianhuo_soft 2008-11-11
  • 打赏
  • 举报
回复
select a.* from 表 a where a.类型 in(select t.类型 from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))

select t.* from 表 t where not exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
marco08 2008-11-11
  • 打赏
  • 举报
回复

--Sorry 上面的理解错了

create table T
(
序号 int,
类型 nvarchar(10),
编号 int
)
insert T
select 1, 'A', 10
union all select 2, 'A', 10
union all select 3, 'A', 11
union all select 4, 'B', 10
union all select 4, 'B', 11

select * from T as tmp
where (select count(*) from T where 类型=tmp.类型 and 编号=tmp.编号)>1
子陌红尘 2008-11-11
  • 打赏
  • 举报
回复
declare @t table(序号 int,类型 varchar(8),编号 int)
insert into @t values(1,'A',10)
insert into @t values(2,'A',10)
insert into @t values(3,'A',11)
insert into @t values(4,'B',10)
insert into @t values(5,'B',11)


--表中的记录序号1、2、3所代表的记录都是不合格的记录?还是仅仅1、2这两条是不合格记录?
--如果是前者: 

--1:查询出表中类型相同,编号相同的记录(不合格的记录)
select a.* from @t a where a.类型 in(select t.类型 from @t t where exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
/*
序号 类型 编号
----------- -------- -----------
1 A 10
2 A 10
3 A 11
*/

--2:查询出表中类型相同,但编号不相同的记录(合格的记录)
select a.* from @t a where a.类型 not in(select t.类型 from @t t where exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
/*
序号 类型 编号
----------- -------- -----------
4 B 10
5 B 11
*/



--如果是后者: 

--1:查询出表中类型相同,编号相同的记录(不合格的记录)
select t.* from @t t where exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
/*
序号 类型 编号
----------- -------- -----------
1 A 10
2 A 10
*/


--2:查询出表中类型相同,但编号不相同的记录(合格的记录)
select t.* from @t t where not exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)

/*
序号 类型 编号
----------- -------- -----------
3 A 11
4 B 10
5 B 11
*/
csdyyr 2008-11-11
  • 打赏
  • 举报
回复
DECLARE @TB TABLE(ID INT, COL VARCHAR(2), COL2  INT)
INSERT @TB
SELECT 1, 'A', 10 UNION ALL
SELECT 2, 'A', 10 UNION ALL
SELECT 3, 'A', 11 UNION ALL
SELECT 4, 'B', 10 UNION ALL
SELECT 4, 'B', 11

SELECT * FROM @TB AS A WHERE EXISTS(SELECT 1 FROM @TB WHERE COL=A.COL AND COL2=A.COL2 AND ID<>A.ID)
/*
ID COL COL2
----------- ---- -----------
1 A 10
2 A 10
*/
SELECT * FROM @TB AS A WHERE NOT EXISTS(SELECT 1 FROM @TB WHERE COL=A.COL AND COL2=A.COL2 AND ID<>A.ID)
/*
ID COL COL2
----------- ---- -----------
3 A 11
4 B 10
4 B 11
*/
昵称被占用了 2008-11-11
  • 打赏
  • 举报
回复
1
select * from tab a
where exists (
select 1 from tab
where 类型=a.类型 and 编号=a.编号
and 序号<>a.序号
)


2
select * from tab a
where not exists (
select 1 from tab
where 类型=a.类型 and 编号=a.编号
and 序号<>a.序号
)
子陌红尘 2008-11-11
  • 打赏
  • 举报
回复
表中的记录序号1、2、3所代表的记录都是不合格的记录?还是仅仅1、2这两条是不合格记录?

如果是前者:

select a.* from 表 a where a.类型 in(select t.类型 from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))

select a.* from 表 a where a.类型 not in(select t.类型 from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))


如果是后者:

select t.* from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)

select t.* from 表 t where not exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
marco08 2008-11-11
  • 打赏
  • 举报
回复
select * from T as tmp
where exists(select * from T where 类型=tmp.类型 and 编号=tmp.编号)

select * from T as tmp
where exists(select * from T where 类型=tmp.类型 and 编号<>tmp.编号)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧