- 編輯我的問題*
我有一組表。當我在第二個表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
明顯地忽略了table3,因爲它沒有被使用;試圖保持簡單。謝謝。 – ChrisH