また、レコードをさらにフィルタリングするために使用される AND 句も表示されます。これらのフィルタリング アクションを where 句に追加すると、結合が左結合から内部結合に変わるため、これは外部結合を扱う上で非常に重要です (where t.idfield が null のようなものでない限り)。
以下に、これがどのように機能するか、およびフィルタリング句を適切な場所に配置することが重要である理由を示します。
create table #test ( test1id int, test varchar (10)) create table #test2 ( test2id int, test1id int, test2 varchar (10))
insert into #test (test1id, test)
select 1, 'Judy'
union all
select 2, 'Sam'
union all
select 3, 'Nathan'
insert into #test2 (test2id, test1id, test2)
select 1,1,'hello'
union all
select 2,1,'goodbye'
union all
select 3,2,'hello'
select * from #test t
left join #test2 t2 on t.test1id = t2.test1id
where test2 = 'goodbye'
--result set
--test1id test test2id test1id test2
--1 Judy 2 1 goodbye
select * from #test t
left join #test2 t2 on t.test1id = t2.test1id
and test2 = 'goodbye'
--result set
--test1id test test2id test1id test2
--1 Judy 2 1 goodbye
--2 Sam NULL NULL NULL
--3 Nathan NULL NULL NULL
where some field is null (null にならないフィールドを選択すると仮定) を使用して、次のように最初のテーブルではなく 2 番目のテーブルのレコードを取得できます。
select * from #test t
left join #test2 t2 on t.test1id = t2.test1id
where test2id is null
--result set
--test1id test test2id test1id test2
--3 Nathan NULL NULL NULL