4

Assuming I have 2 tables

Table a
brand    Fruit         edible   color   size
farmers  banana        yes      yellow  null
fresh    banana        yes      red     10
bounty   banana        null     green   2
farmers  apple         yes      red     5
organic  grapes        null     violet  5
love     strawberry    yes      null    5
flow     lavander      no       null    null

Table b
boxId   fruit     edible    color   size
10100   banana     yes       yellow  9
19299   banana     yes       red     10
10992   apple      yes       red     5
10299   grapes     yes       red     5 
01929   lavander   no        violet  3

is it possible to join Table a and b even with the rule that:if there are null values, continue evaluating the remaining columns by skipping the null column.

select a.brand, b.boxId from a prod
inner join b box on a.fruit = b.fruit
where a.edible = b.edible and 
a.color = b.color and 
a.size = b.size

brand       boxID
farmers      10100
fresh        19299
. . .
3
  • What do you mean null? all fields if there's a null or specific field only? Commented Feb 3, 2017 at 7:09
  • all fields there's a null Commented Feb 3, 2017 at 7:20
  • 3
    Surely that's what a LEFT [OUTER] JOIN is for Commented Feb 3, 2017 at 7:54

2 Answers 2

6

Yes like this

select a.brand, b.boxId 
from a prod
inner join b box on a.fruit = b.fruit
where ( a.edible = b.edible OR a.edible IS NULL OR b.edible IS NULL ) and 
      ( a.color = b.color OR a.color IS NULL OR b.color IS NULL ) and 
      ( a.size = b.size OR a.size IS NULL OR b.size IS NULL)
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

select a.brand,b.boxId from
(select a.brand,a.Fruit,a.edible,a.color,a.size from prod) as a
inner join
(select * from box) as b
 on a.fruit = b.fruit 
where a.color is not null and a.edible is not null and a.size is not null
and b.color is not null and b.edible is not null and b.size is not null

2 Comments

It will show all not null you can change is null if you want all null value between two tables.
thanks, but I accepted JaydipJ because he posted first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.