I want to extract some data from my shop's database. The data I want is in four tables: ps_order_detail, ps_orders, ps_carrier and ps_customer. The main table is ps_order_detail. For each record of ps_order_detail I want to attach corresponding values from other tables based on id's: id_order, id_customer and id_carrier. The problem is that the base table (ps_order_detail) contains only id_order and the other id's are in ps_orders table. Do you know how can I solve this?
            Asked
            
        
        
            Active
            
        
            Viewed 25 times
        
    0
            
            
        
        Isaac Bennetch
        
- 11,830
 - 2
 - 32
 - 43
 
        Wojtek Wencel
        
- 2,257
 - 6
 - 31
 - 65
 
- 
                    What have you tried? This is a pretty basic `JOIN` query. – Gordon Linoff Sep 20 '17 at 16:15
 - 
                    You can `JOIN` table A to B, then B to C, then B to D, etc.. They don't all have to go directly to A. – Aaron Dietz Sep 20 '17 at 16:16
 - 
                    I'd assume, select from `ps_order_detail` join to `ps_orders` and then join the others from there using `ps_orders` – Nope Sep 20 '17 at 16:17
 - 
                    Oh okay. would it be something like this https://stackoverflow.com/questions/9853586/how-can-i-join-multiple-sql-tables-using-the-ids? – Wojtek Wencel Sep 20 '17 at 16:18
 - 
                    just join all 4 tables like @AaronDietz said – Ryan Gadsdon Sep 20 '17 at 16:20
 - 
                    Google: `JOIN`. – Eric Sep 20 '17 at 16:22
 
1 Answers
1
            Your question is unclear on the data layouts of the tables, but the query would look something like this:
select . . . 
from ps_order_detail od join
     ps_orders o
     using (id_order) join
     ps_customer c
     using (id_customer) join
     ps_carrier ca
     using (id_carrier)
        Gordon Linoff
        
- 1,242,037
 - 58
 - 646
 - 786