Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have table name "order_booking". It has one field name is 'type' and value of type like 'commercial,packaging,order'.

I have another three table 'commercial_estimation','packaging_estimation' and 'order_estimation'.

My question is that i want to join 'order_booking' table with this three table but conditional means if i have a type 'commercial' then join with 'commercial_estimation' table,if i have type 'packaging' then join with 'packaging_estimation' table and if i have type 'Order' then join with 'order_estimation' table. So, i can only join with any one table based on 'type'.

What I have tried:

SELECT a.type FROM  order_booking a 
    (Case when type a.type = 'commercial' Then 'Inner Join commercial_estimation  b on a.id = b.oid' 
          when type a.type = 'packaging' Then 'Inner Join packaging_estimation  b on a.id = b.oid' 
          else  type a.type = 'order' Then 'Inner Join order_estimation  b on a.id = b.oid' END) 
Posted
Updated 18-Apr-18 4:03am

1 solution

How is about this:

SQL
SELECT 
  a.type,
   
FROM  order_booking a
LEFT JOIN commercial_estimation b ON a.type = 'commercial' AND b.oid = a.id
LEFT JOIN packaging_estimation c ON a.type = 'packaging' AND c.oid = a.id
LEFT JOIN order_estimation d ON a.type = 'order' AND d.oid = a.id


To have access to the Details you can also use COALESCE in case the datatype do match and you like to have one field for the three Details.
 
Share this answer
 
Comments
Maciej Los 23-May-18 17:24pm    
Well, seems your Sql knowledge growed up!
;)
[no name] 24-May-18 2:50am    
Thank you Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900