你正在尋找一個鏈接的加入與此類似
select * from table3
join table2 on table3.table2ID = table2.table2ID
join table1 on table2.table1ID = table1.table1ID
where table1.ID = 4
這隻會返回表3的記錄,如果有匹配的表2和表1的紀錄。聯接將結果篩選出其中加入匹配,並且WHERE過濾器將只從結果中選擇其中table1的ID = 4
工作示例我颳起(嘿我有時間殺)
create table #table1 (table1ID int)
create table #table2 (table2ID int, table1ID int)
create table #table3 (table3ID int, table2ID int)
insert into #table1 values(1)
insert into #table1 values(2)
insert into #table1 values(3)
insert into #table1 values(4)
insert into #table2 values(10, 1)
insert into #table2 values(11, 2)
insert into #table2 values(12, 3)
insert into #table2 values(13, 4)
insert into #table3 values(20, 10)
insert into #table3 values(21, 11)
insert into #table3 values(22, 12)
insert into #table3 values(23, 13)
-- all joined records
select * from #table3
join #table2 on #table3.table2ID = #table2.table2ID
join #table1 on #table2.table1ID = #table1.table1ID
-- only where table1 ID = 4
select * from #table3
join #table2 on #table3.table2ID = #table2.table2ID
join #table1 on #table2.table1ID = #table1.table1ID
where #table1.table1ID = 4
drop table #table1
drop table #table2
drop table #table3
這給你
table3ID table2ID table2ID table1ID table1ID
----------- ----------- ----------- ----------- -----------
20 10 10 1 1
21 11 11 2 2
22 12 12 3 3
23 13 13 4 4
table3ID table2ID table2ID table1ID table1ID
----------- ----------- ----------- ----------- -----------
23 13 13 4 4
你的問題無關,與SQL Management Studio中。雖然,很顯然你正在使用SQL Server並通過SQL Management Studio訪問服務器。 – TechTravelThink 2010-11-02 12:44:57
固定標籤以匹配問題。 – invert 2010-11-02 12:53:13