我有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)
我期望的結果是得到在#temp表和active_tag = 1 #table_link即所有行。
id | name | #temp2_id |
1 | ABC | 2 |
2 | XYZ | NULL |
我試着查詢
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
在此查詢我得到的只有ABC行only.I使用左連接,爲什麼XYZ行不get.Currently結果來了這樣
id | name | #temp2_id |
1 | ABC | 2 |
請解釋爲什麼它不起作用?
是的,我用左爲什麼第二行不存在? –
同樣的結果:(。 –