Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join a table using on multiple columns using a OR syntax [duplicate]

So what I'm trying to do is :-

SELECT * FROM TableA
JOIN TableB ON TableA.OriginPhoneNumber=TableB.Id OR TableA.DestinationPhoneNumber=TableB.Id

Rather strange query I know! But I'm trying to replicate this in EntityFramework/Linq - looking at all the samples I can see a pretty easy way to do it when the join is using an AND (using anonymous types) but does the same result exist for a OR join?

like image 226
John Mitchell Avatar asked Dec 11 '25 03:12

John Mitchell


1 Answers

Just do a cross join with a where clause

var results = from a in db.TableA
              from b in db.TableB
              where a.OriginPhonenumber == b.Id 
                    || a.DestinationPhoneNumber == b.Id
              select new { A = a, B = b };

It's doubtful that an or in a join condition would be more efficient than this, but it's likely that either would result in the same execution plan. Performance aside it will give the same results.

like image 126
juharr Avatar answered Dec 13 '25 17:12

juharr



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!