1.子查詢概念 
(1)就是在查詢的where子句中的判斷依據是另一個查詢的結果,如此就構成了一個外部的查詢和一個內部的查詢,這個內部的查詢就是自查詢。 
(2)自查詢的分類 
1)獨立子查詢 
->獨立單值(標量)子查詢(=) 
復制代碼 代碼如下:
 
Select 
testID,stuID,testBase,testBeyond,testPro 
from Score 
where stuID=( 
select stuID from Student where stuName='Kencery' 
) 
復制代碼 代碼如下:
 
Select 
testID,stuID,testBase,testBeyond,testPro 
from Score 
where stuID in( 
select stuID from Student where stuName='Kencery' 
) 
復制代碼 代碼如下:
 
use Test 
go 
create table testNum1 
( 
Num1 int 
); 
create table testNum2 
( 
Num2 int 
); 
insert into testNum1 values(1),(2),(3) 
insert into testNum2 values(4),(5) 
復制代碼 代碼如下:
 
insert into testNum1 values(4),(5),(6),(7),(8),(9),(0) 
復制代碼 代碼如下:
 
select t1.num1,t2.num2 from testNum1 as t1 cross join testNum2 as t2 
復制代碼 代碼如下:
 
Selects1.stuID, 
s1.stuName, 
s1.stuSex, 
s2.testBase, 
s2.testBeyond 
from Student as s1 
inner join Score as s2 
on s1.stuID=s2.stuID 
where s1.stuIsDel=0; 
復制代碼 代碼如下:
 
create table tblMain 
( 
ID int, 
name nvarchar(20), 
fid int 
); 
create table tblOther 
( 
ID int, 
name nvarchar(20) 
) 
insert into tblMain values(1,'張三',1),(2,'李四',2) 
insert into tblOther values(1,'C++'),(2,'.net'),(3,'java') 
select * from 
tblMain as t1 
inner join 
tblOther as t2 
on 
t1.fid=t2.id 
復制代碼 代碼如下:
 
select * from 
tblMain as t1 
right join tblOther as t2 
on t1.fid=t2.id 
新聞熱點
疑難解答