- 编辑我的问题 *
我有一组桌子。当我对第二个表 t2 进行过滤时,我仍然希望获得 t1 的所有行。
SQL脚本如下。我觉得我在修补时越来越近了,但我就是做不到。
简而言之,我需要 t2 的行(如果适用),但 t1 的所有行在其他列中都带有空值。
谢谢。
创建表 t1 ( id int identity(1,1), parentName varchar(20) null )
创建表 t2 ( id int identity(1,1), t1id int not null, childName varchar(20) null )
创建表 t3 ( id int identity(1,1), t2id int not null, gChildName varchar(20) null )
插入 t1 ( parentName ) 值 ( 'bob' )
插入 t1 ( parentName ) 值 ( 'john' )
插入 t2 ( childName, t1id ) 值 ( 'irving', 1 )
插入 t2 ( childName, t1id ) 值 ( 'parna', 1 )
插入 t2 ( childName, t1id ) 值 ( 'mike', 1 )
选择
t1.id,
t1.parentName,
t2.id,
t2.childName
从 t1 左外连接 t2
在 t2.t1id = t1.id
其中 t2.childName = 'mike'
-- 我想要的是:
-- 1,鲍勃,3,迈克
-- 2,约翰,空,空
删除表 t3
删除表 t2
删除表 t1