Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server conditional join

I have to choose the according column to join basing on a value that can be null. In detail:

SELECT shoporders.orderid, 
   shopaddresses.companyname, 
   shopaddresses.firstname, 
   shopaddresses.lastname, 
   shopaddresses.address1,  
FROM   shoporders 
   INNER JOIN shopaddresses 
           ON shoporders.InvoiceAddressId = shopaddresses.addressid
where orderid = 110
order by shoporders.createddate desc

if shoporders.InvoiceAddressId is null then I have to use shoporders.DeliveryAddressId

Any clue?

Thanks in advance

like image 481
Attila Avatar asked Mar 17 '26 09:03

Attila


1 Answers

ON coalesce(shoporders.InvoiceAddressId, shoporders.DeliveryAddressId) = shopaddresses.addressid
like image 107
D'Arcy Rittich Avatar answered Mar 19 '26 00:03

D'Arcy Rittich