2015-04-23 21 views
1
  • 編輯我的問題*

我有一組表。當我在第二個表t2上過濾時,我仍然想要獲取t1的所有行。SQL返回父行,即使沒有子行

SQL腳本如下。我覺得我在修補時會越來越近,但我無法做到這一點。

總之,我需要t2的行適用時,但所有t1的行與其他列中的空值。

謝謝。

 
create table t1 (id int identity(1,1), parentName varchar(20) null) 
create table t2 (id int identity(1,1), t1id int not null, childName varchar(20) null) 
create table t3 (id int identity(1,1), t2id int not null, gChildName varchar(20) null) 

insert into t1 (parentName) values ('bob') 
insert into t1 (parentName) values ('john') 

insert into t2 (childName, t1id) values ('irving', 1) 
insert into t2 (childName, t1id) values ('parna', 1) 
insert into t2 (childName, t1id) values ('mike', 1) 

select 
     t1.id, 
     t1.parentName, 
     t2.id, 
     t2.childName 
from t1 left outer join t2 
     on t2.t1id = t1.id 
where t2.childName = 'mike' 

-- what i'd LIKE is: 
-- 1, bob, 3, mike 
-- 2, john, null, null 

drop table t3 
drop table t2 
drop table t1 
+0

明顯地忽略了table3,因爲它沒有被使用;試圖保持簡單。謝謝。 – ChrisH

回答

4

正如其他人所說,你可以移動T3篩選出整體WHERE條款,並把它放到JOIN,這阻止它有效地把你的外部連接成一個僞內部連接(這是因爲無在NULL值都不能匹配WHERE標準除了IS NULL

這是給你的示例代碼非常簡單的改變 - 只是改變WHEREAND

create table t1 (id int identity(1,1), parentName varchar(20) null) 
create table t2 (id int identity(1,1), t1id int not null, childName varchar(20) null) 
create table t3 (id int identity(1,1), t2id int not null, gChildName varchar(20) null) 

insert into t1 (parentName) values ('bob') 
insert into t1 (parentName) values ('john') 

insert into t2 (childName, t1id) values ('irving', 1) 
insert into t2 (childName, t1id) values ('parna', 1) 
insert into t2 (childName, t1id) values ('mike', 1) 

select 
    t1.id, 
    t1.parentName, 
    t2.id, 
    t2.childName 

from t1 
    left outer join t2 on t2.t1id = t1.id and t2.childName = 'mike' 

drop table t3 
drop table t2 
drop table t1 
+0

對,(聽起來不太嚴重,但我使用外部連接),但是當我在不存在的t3行上過濾時,我得到零行。我需要t1信息和其餘列的空值。 – ChrisH

+0

好吧,獲得準確響應的好方法是公開您已經嘗試過的代碼塊示例。沒有辦法告訴你已經使用了外連接。我編輯了我的答案,爲您的t3過濾器添加了正確的位置。 – CactusCake

+0

不夠公平,對不起。讓我編輯我的第一個問題... – ChrisH

0

如果要加入2個或多個表,並希望從第一個結果,即使沒有從第二(或第三等)的比賽,你只需要改變的加入爲左連接。像

SELECT * FROM東西TABLE1一個

LEFT JOIN TABLE2 B開A.ID = B.RELATEDID

0

您可以簡單地使用左連接:

Select t1.id,t1.name,t2.id id2,t2.name name2,t3.id id3,t3.name name3 
From t1 left join 
    t2 on t1.id=t2.t1id left join 
    t3 on t3.t2id=t2.id 
Where your condition here 
2

這聽起來像你可能會使用左連接,但會根據where子句刪除行。例如:

Select * from Table1 a 
left join Table2 b 
on a.ID = b.ID 
where b.name like 'A%' 

將下降從表1中的所有行那裏是在表2中沒有匹配,即使你離開加入了(因爲在當b.name爲空條件不符合)。

爲了避免這種情況,把你的條件在連接代替,就像這樣:

Select * from Table1 a 
left join Table2 b 
on a.ID = b.ID and b.name like 'A&' 

或添加ISNULL到您的where子句,像這樣:

Select * from Table1 a 
left join Table2 b 
on a.ID = b.ID 
where ISNULL(b.name, 'A') like 'A%' 

編輯:現在你已發佈您的查詢,這裏有一個具體的答案:只要將「where」改爲「and」,它就會返回您指定的結果。

select 
    t1.id, 
    t1.parentName, 
    t2.id, 
    t2.childName 
from #t1 t1 left outer join #t2 t2 
    on t2.t1id = t1.id 
    and t2.childName = 'mike' 
+0

是的,我想我將不得不從查詢中刪除最終條件(t3.id = 1),並在客戶端中對其進行過濾;一切正常,直到我開始過濾最後一個表的值。我想這是可以預料的,但我很樂意*總是*得到table1的信息。 – ChrisH

+0

只需把它放在連接中 - 應該找到你正在尋找的東西。例如'從t1選擇* ....在t2.ID = t3.t2ID和t3.ID = 1'上左連接t3 – APH

+0

編輯我的第一個問題,試圖使其更清晰。希望這可以幫助。謝謝。 – ChrisH

相關問題