I 3つのテーブル左にジョインした後、sqlを使用して不正な結果を内部結合しますか?
drop table #temp;
create table #temp(id int,name varchar(50))
insert into #temp values(1,'ABC');
insert into #temp values(2,'XYZ');
select * from #temp
drop table #table_link;
create table #table_link(id int,temp_id int,temp2_id int)
insert into #table_link values(1,1,1);
insert into #table_link values(2,1,2);
select * from #table_link
drop table #temp2;
create table #temp2(id int,active_tag bit);
insert into #temp2 values(1,0)
insert into #temp2 values(2,1)
Iを有する結果であること#table_linkと#TEMPテーブルとactive_tag = 1のすべての行を取得所望。
id | name | #temp2_id |
1 | ABC | 2 |
2 | XYZ | NULL |
私はクエリがXYZ行がget.Currently結果は、この
のように来ていない理由を私はABC行only.Iは、参加左慣れるこのクエリでselect * from #temp t
left join #table_link tl on tl.temp_id=t.id
inner join #temp2 t2 on t2.id=tl.temp2_id and t2.active_tag=1
で試してみました
id | name | #temp2_id |
1 | ABC | 2 |
なぜ機能しないのですか?
はい私は左の2番目の行が存在しない理由を使用しますか? –
同じ結果:(。 –