T-SQL left join not returning null columns

拟墨画扇 提交于 2020-01-26 03:58:09

问题


I have 2 tables in T-SQL one with 10 records and one with 11.

select tbl.unit, tbl2.unid 
from tbl1
left join tbl2 on tbl2.UNID = tbl1.UNIT
where tbl2.Status = 'Main'
group by unit, UNID

This only returns 10 records when tbl1 has 11 records. I was expecting the missing record to show a value if UNIT but null for UNID but is just is not there.

I cannot figure out why this is the case


回答1:


So why would a LEFT JOIN not show all the records from a left side of the join.

Is a bug?

Most likely not.

Lets look at a simplified example.

TableA has 3 records.

ID    ColA
1     Foo
2     Bar
3     Buz

TableB has 2 records

ID    ColB
4     Foo
5     Bar

An INNER JOIN on ColA & ColB would return 2 records.
Only those where a match is found.

SELECT ColA, ColB 
FROM TableA a
JOIN TableB b ON b.ColB = a.ColA

Returns:

ColA    ColB
Foo     Foo
Bar     Bar

A LEFT JOIN would return 3 records.
With a NULL for the right side for the unmatched.

SELECT ColA, ColB 
FROM TableA a
LEFT JOIN TableB b ON b.ColB = a.ColA

Returns:

ColA    ColB
Foo     Foo
Bar     Bar
Buz     null

But what happens if a criteria is used in the WHERE clause for the right side?

SELECT ColA, ColB 
FROM TableA a
LEFT JOIN TableB b ON b.ColB = a.ColA
WHERE b.ColB IN ('Foo', 'Bar', 'Buz')

Returns:

ColA    ColB
Foo     Foo
Bar     Bar

What? Where is the 'Buz'?

Can you guess why that LEFT JOIN seems to behave like an INNER JOIN?

The solution is to put such criteria in the ON clause.

SELECT ColA, ColB 
FROM TableA a
LEFT JOIN TableB b 
   ON b.ColB = a.ColA AND b.ColB IN ('Foo', 'Bar', 'Buz')

Or do put the criteria in the WHERE, but also allow NULL.

SELECT ColA, ColB 
FROM TableA a
LEFT JOIN TableB b 
   ON b.ColB = a.ColA
WHERE (b.ColB IN ('Foo', 'Bar', 'Buz') OR b.ColB IS NULL)

Returns:

ColA    ColB
Foo     Foo
Bar     Bar
Buz     null

Now we have Buz back.



来源:https://stackoverflow.com/questions/59038630/t-sql-left-join-not-returning-null-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!