jestem całkiem pewny, że przyłączyć nawet nie robić, co chcesz. Jeśli w tabeli a z zerowym qid i 100 rekordów w tabeli b z zerowym znakiem qid znajduje się 100 rekordów, to połączenie powinno zostać wykonane w formie połączenia krzyżowego i dać 10 000 wyników dla tych rekordów. Jeśli spojrzeć na poniższy kod i uruchomić przykłady, myślę, że ostatni z nich jest prawdopodobnie bardziej wynikiem ustawić zamierzałeś:
create table #test1 (id int identity, qid int)
create table #test2 (id int identity, qid int)
Insert #test1 (qid)
select null
union all
select null
union all
select 1
union all
select 2
union all
select null
Insert #test2 (qid)
select null
union all
select null
union all
select 1
union all
select 3
union all
select null
select * from #test2 t2
join #test1 t1 on t2.qid = t1.qid
select * from #test2 t2
join #test1 t1 on isnull(t2.qid, 0) = isnull(t1.qid, 0)
select * from #test2 t2
join #test1 t1 on
t1.qid = t2.qid OR (t1.qid IS NULL AND t2.qid IS NULL)
select t2.id, t2.qid, t1.id, t1.qid from #test2 t2
join #test1 t1 on t2.qid = t1.qid
union all
select null, null,id, qid from #test1 where qid is null
union all
select id, qid, null, null from #test2 where qid is null
Dla jakiej bazy danych (w tym wersji)? –
Tylko SQL Server i Access mają 'ISNULL()', więc zakładam, że SQL Server –
Baza danych: sql 2005 – Rico