34,590
社区成员
发帖
与我相关
我的任务
分享
create table S(SNO varchar(4),CNO varchar(4), SCORE int)
insert into S select 'S1','C1','90'
union all select 'S1','C2','85'
union all select 'S2','C5','57'
union all select 'S2','C6','80'
union all select 'S2','C7','80'
union all select 'S3','C1','80'
union all select 'S3','C2','89'
union all select 'S3','C4','23'
union all select 'S4','C1','34'
union all select 'S4','C2','79'
union all select 'S4','C3','34'
union all select 'S5','C2','89'
;with tb as
(
select num=count(1),a.SNO
from S a,S b
where a.CNO=b.CNO and b.SNO='s1'
group by a.SNO)
select SNO from tb where num>=(select num from tb where SNO='s1')
--SNO
------
--S1
--S3
--S4
--
--(3 行受影响)
declare @table table([SNO] varchar(2),[CNO] varchar(2),[SCORE] int)
insert @table
select 'S1','C1',90 union all
select 'S1','C2',85 union all
select 'S2','C5',57 union all
select 'S2','C6',80 union all
select 'S2','C7',80 union all
select 'S3','C1',80 union all
select 'S3','C2',89 union all
select 'S3','C4',23 union all
select 'S4','C1',34 union all
select 'S4','C2',79 union all
select 'S4','C3',34 union all
select 'S5','C2',89
select SNO
from
(
select SNO,count(1) as times from @table
where CNO in (select CNO from @table where SNO = 'S1')
group by SNO
) t
where times = (select count(1) from @table where SNO = 'S1')
--结果
--------------
S1
S3
S4
-- =============================================
-- Author: T.O.P
-- Create date: 2009/12/01
-- Version: SQL SERVER 2005
-- =============================================
declare @tb table([SNO] varchar(2),[CNO] varchar(2),[SCORE] int)
insert @tb
select 'S1','C1',90 union all
select 'S1','C2',85 union all
select 'S2','C5',57 union all
select 'S2','C6',80 union all
select 'S2','C7',80 union all
select 'S3','C1',80 union all
select 'S3','C2',89 union all
select 'S3','C4',23 union all
select 'S4','C1',34 union all
select 'S4','C2',79 union all
select 'S4','C3',34 union all
select 'S5','C2',89
select distinct a.sno
from @tb A
where sno<>'S1' and not exists(
select 1 from @tb b where sno='S1' and
not exists(select 1 from @tb c where a.sno = c.sno and b.cno=c.cno)
)
--测试结果:
/*
sno
----
S3
S4
(所影响的行数为 2 行)
*/
create table tb([SNO] varchar(2),[CNO] varchar(2),[SCORE] int)
insert tb
select 'S1','C1',90 union all
select 'S1','C2',85 union all
select 'S2','C5',57 union all
select 'S2','C6',80 union all
select 'S2','C7',80 union all
select 'S3','C1',80 union all
select 'S3','C2',89 union all
select 'S3','C4',23 union all
select 'S4','C1',34 union all
select 'S4','C2',79 union all
select 'S4','C3',34 union all
select 'S5','C2',89
go
select sno from tb where sno <> 'S1' and cno in
(select cno from tb where sno = 'S1')
group by sno having count(1) >= (select count(1) from tb where sno = 'S1')
drop table tb
/*
sno
----
S3
S4
(所影响的行数为 2 行)
*/
SNO
----
S3
S4
(2 行受影响)
declare @table table([SNO] varchar(2),[CNO] varchar(2),[SCORE] int)
insert @table
select 'S1','C1',90 union all
select 'S1','C2',85 union all
select 'S2','C5',57 union all
select 'S2','C6',80 union all
select 'S2','C7',80 union all
select 'S3','C1',80 union all
select 'S3','C2',89 union all
select 'S3','C4',23 union all
select 'S4','C1',34 union all
select 'S4','C2',79 union all
select 'S4','C3',34 union all
select 'S5','C2',89
select a.SNO
from @table a,@table b
where b.SNO='S1'
And a.SNO!=b.SNO
And a.CNO=b.CNO
group by a.SNO
having COUNT(*)>1
create table S(SNO varchar(4),CNO varchar(4), SCORE int)
insert into S select 'S1','C1','90' union all
select 'S1','C1',90 union all
select 'S1','C2',85 union all
select 'S2','C5',57 union all
select 'S2','C6',80 union all
select 'S2','C7',80 union all
select 'S3','C1',80 union all
select 'S3','C2',89 union all
select 'S3','C4',23 union all
select 'S4','C1',34 union all
select 'S4','C2',79 union all
select 'S4','C3',34 union all
select 'S5','C2',89
--查询语句
select distinct sno from s where
cno in(select distinct cno from s where sno='S1')