Bartman
Bartman

Reputation: 97

Join two tables using OR operator

How do i write a query that will join two tables using an Or operator For example:

select upc,ean,productName
from maintable m1 left join maintable2 m2
on m2.upc = m1.upc OR m2.ean = m1.ean

Upvotes: 0

Views: 202

Answers (1)

Elzair
Elzair

Reputation: 452

Well, you can try using a UNION.

select upc,ean,productName
from maintable m1 left join maintable2 m2
on m2.upc = m1.upc
union
select upc,ean,productName
from maintable m11 left join maintable m22
on m22.ean = m11.ean

Does that work out for you? By default, the UNION operator will only return distinct rows, so do not worry about returning duplicates.

Upvotes: 1

Related Questions