34,576
社区成员
发帖
与我相关
我的任务
分享
use tempdb
if object_id('t') is not null drop table t
go
create table t([Sno] varchar(50),[Cno] INT,[Grade] INT)
insert into t
select '95001',1,92 union all
select '95001',2,85 union all
select '95001',3,88 union all
select '95001',4,23 union all
select '95001',5,34 union all
select '95001',6,56 union all
select '95001',7,86 union all
select '95001',8,88 union all
select '95002',2,90 union all
select '95002',3,80 union all
select '95003',1,50 union all
select '95003',3,98 union all
select '95005',3,NULL
go
select * from t
select
distinct Sno
from
tb t
where
not exists
(selec
1
from
tb a
where
Sno='95002'
and
not exists (select 1 from tb where Sno=t.Sno and Cno=a.Cno)
use tempdb
if object_id('t') is not null drop table t
go
create table t([Sno] varchar(50),[Cno] INT,[Grade] INT)
insert into t
select '95001',1,92 union all
select '95001',2,85 union all
select '95001',3,88 union all
select '95001',4,23 union all
select '95001',5,34 union all
select '95001',6,56 union all
select '95001',7,86 union all
select '95001',8,88 union all
select '95002',2,90 union all
select '95002',3,80 union all
select '95003',1,50 union all
select '95003',3,98 union all
select '95005',3,NULL
go
select sno from t
where sno <> '95002' and cno in (select cno from t where sno = '95002')
group by sno having count(1) = (select count(1) from t where sno = '95002')
---嵌套太多 效率也不高
select
distinct Sno
from
tb t
where
not exists
(selec
1
from
tb a
where
Sno='95002'
and
not exists (select 1 from t where Sno=t.Sno and Cno=a.Cno)
1> select * from t
2> go
Sno |Cno |Grade
----------------------------------------|-----------|-----------
95001 | 1| 92
95001 | 2| 85
95001 | 3| 88
95001 | 4| 23
95001 | 5| 34
95001 | 6| 56
95001 | 7| 86
95001 | 8| 88
95002 | 2| 90
95002 | 3| 80
95003 | 1| 50
95003 | 3| 98
95005 | 3| NULL
(13 rows affected)
1> select distinct Sno
2> from t c
3> where not exists (
4> select 1 from t a
5> where Sno='95002'
6> and not exists (select 1 from t where Sno=c.Sno and Cno=a.Cno)
7> )
8> go
Sno
----------------------------------------
95001
95002
(2 rows affected)
1>
select sno from t where
cno =any (select cno from t where sno = '95002') and sno <> '95002'
group by sno having count(*) >= (select count(*) from t where sno = '95002')
create table t([Sno] varchar(50),[Cno] INT,[Grade] INT)
insert into t
select '95001',1,92 union all
select '95001',2,85 union all
select '95001',3,88 union all
select '95001',4,23 union all
select '95001',5,34 union all
select '95001',6,56 union all
select '95001',7,86 union all
select '95001',8,88 union all
select '95002',2,90 union all
select '95002',3,80 union all
select '95003',1,50 union all
select '95003',3,98 union all
select '95005',3,NULL
go
select sno from t
where sno <> '95002' and cno in (select cno from t where sno = '95002')
group by sno having count(1) = (select count(1) from t where sno = '95002')
drop table t
/*
sno
--------------------------------------------------
95001
(所影响的行数为 1 行)
*/
--貌似没啥高效的写法
select sno from sc
where cno in (select cno from sc where sno = '95002')
and sno <> '95002'
group by sno
having count(*) = (select count(*) from sc where sno = '95002')
declare @t table([Sno] varchar(50),[Cno] INT,[Grade] INT)
insert into @t
select '95001',1,92 union all
select '95001',2,85 union all
select '95001',3,88 union all
select '95001',4,23 union all
select '95001',5,34 union all
select '95001',6,56 union all
select '95001',7,86 union all
select '95001',8,88 union all
select '95002',2,90 union all
select '95002',3,80 union all
select '95003',1,50 union all
select '95003',3,98 union all
select '95005',3,NULL
select * from @t
select distinct c.Sno from @t c where not exists(
select * from @t b where Sno='95002' and not exists(
select * from @t a where a.Cno=b.Cno and a.Sno=c.Sno))
select distinct c.Sno from @t c where not exists(
select * from @t b where Sno='95002' and not exists(
select * from @t a where a.Cno=b.Cno and a.Sno=c.Sno))
use tempdb
if object_id('t') is not null drop table t
go
create table t([Sno] varchar(50),[Cno] INT,[Grade] INT)
insert into t
select '95001',1,92 union all
select '95001',2,85 union all
select '95001',3,88 union all
select '95001',4,23 union all
select '95001',5,34 union all
select '95001',6,56 union all
select '95001',7,86 union all
select '95001',8,88 union all
select '95002',2,90 union all
select '95002',3,80 union all
select '95003',1,50 union all
select '95003',3,98 union all
select '95005',3,NULL
go
select DISTINCT SNO
from t T1
WHERE NOT EXISTS(SELECT 1 FROM T T2 WHERE T2.SNO='95002'
AND T2.CNO NOT IN (SELECT CNO FROM T T3 WHERE T3.SNO=T1.SNO)
) AND T1.SNO<>'95002'
--95001
select sno from sc
where cno in (select cno from sc where sno = '95002')
and sno <> '95002'
group by sno
having count(*) = (select count(*) from sc where sno = '95002')