Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Join Including NULLs

Using SQL Server Management Studio 2016 (v 13.0).

I have two tables with two distinct keys I can use to join, with the catch being that there are mixed NULLs in both PK columns:

Table 1          Table 2    

Acct1  App1      Acct2  App2    Product
-----------      ----------------------
1      A         NULL   A       Bed 
2      B         2      B       Sofa
3      C         3      NULL    Bed 
4      D         4      D       Bed 

Desired result in the joined table, only including those where Product = Bed:

Acct  App  Product
1     A    Bed 
3     C    Bed 
4     D    Bed 

Thank you!

like image 301
jcal Avatar asked Jun 16 '26 21:06

jcal


1 Answers

While I agree @d219's answer should be the correct solution, a different approach could use an or in the join such like:

select Acct1,App1,Product 
from table1 inner join table2
on App1=App2 or Acct1=Acct2 
where Product='Bed'

See this post for discussion on using the or join.

like image 69
cacti5 Avatar answered Jun 19 '26 11:06

cacti5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!