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
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 行受影響)
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'
SELECT SNO FROM
(
SELECT B.SNO,B.CNO FROM S A JOIN S B ON A.CNO=B.CNO AND A.SNO='S1'
)TEMP GROUP BY SNO
HAVING COUNT(1)=(SELECT COUNT(1) FROM S WHERE SNO='S1')
SNO
--------------------
S1
S3
S4
(3 行受影響)
SELECT 'S1' SNO,'C1' CNO,'90' SCORE INTO TB 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 TB A WHERE NOT EXISTS
(SELECT 1 FROM TB B WHERE SNO='S1' AND NOT EXISTS
(SELECT * FROM TB C WHERE C.SNO=A.SNO AND C.CNO=B.CNO))
---------
S1
S3
S4
---------------------------------------------
--> Author : js_szy
--> Target : 各位大大,小卒就是想要一朵花
--> Date : 2009-12-01 13:09:42
--> Version: SQL Server 2005
---------------------------------------------
--> 測試數據: @tb
declare @tb table (SNO varchar(2),CNO varchar(2),SCORE int)
insert into @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 sno from @tb where cno in(
select cno from @tb where sno='s1'
)
group by sno having(count(*)=(select count(cno) from @tb where sno='s1'))
sno
----
S1
S3
S4
(3 行受影響)
.子查詢
這里不知道寫什么重點,我覺得子查詢分2種吧。一種是獨立的子查詢,和外部查詢無關,它只為外部查詢執行一次足矣.還有一種是相關的子查詢,
它是外部查詢沒執行一行它就跑一次,是動態的.
我這里舉個例子:
--學生表
create table #s(sno int,sname varchar(10))
--選課表
create table #sc(sno int,cno int,score int)
--課程表
create table #c(cno int,cname varchar(10))
--插入數據
insert #s select
1,'a' union all select
2,'b' union all select
3,'c'
insert #c select
1,'English' union all select
2,'Chinese' UNION ALL SELECT
3,'Computer'
insert #sc select
1,1,90 union all select
1,2,89 union all select
1,3,87 union all select
2,2,99 union all select
3,1,76 union all select
3,3,65
---查詢出選修了全部課程的學生姓名-------
--------無關聯子查詢----------
select sname
from #s join #sc on #s.sno=#sc.sno
group by sname
having COUNT(cno)=(select COUNT(*) from #c)--這里就是無關聯的子查詢
-------相關子查詢------------
select sname
from #s s
where not exists(select * from #c c where not exists
(select *from #sc where sno=s.sno and cno=c.cno)
)
------結果-----------
/*
sname
----------
a
*/
上面通過學生選課的經典例子說明了用不同的子查詢解決問題的一個用法。
雙重否定的語句
-- =============================================
-- 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 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
----
S1
S3
S4
(3 row(s) affected)
*/
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')
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
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 行)
*/
-- =============================================
-- 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 行)
*/
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。