Home > OS >  Join multiples tables query SQL
Join multiples tables query SQL

Time:05-04

I need to simulate a train network. I have 3 main tables in my database, and I need to create one main table with different metrics of each table.

These are my 3 tables with their columns:

CodePudding user response:

Try This :

    select 
    t1.date, t1.brand, t1.to, t1.met`enter code here`ric1, t1.metric2, 
    t2.metric3, t3.metric4 
into 
    tain_with_metrics
from 
    table_1 t1 
join 
    table_2 t2 on t1.date = t2.date 
               and t1.brand = t2.brand 
               and t1.to = t2.to 
join 
    table_3 t3 on t3.date = t2.date 
               and t3.method = t2.method 
               and t3.type = t2.type;

CodePudding user response:

Can you try this? Is this as per your requirement? @pierredupont

select 
    t1.date, t1.brand, t1.to, t1.metric1, t1.metric2, 
    t2.metric3, t3.metric4 
into 
    tain_with_metrics
from 
    table_1 t1 
left join 
    table_2 t2 on t1.date = t2.date 
               and t1.brand = t2.brand 
               and t1.to = t2.to 
left join 
    table_3 t3 on t3.date = t1.date 
               and t3.from = t2.from 
               and t3.to = t1.to
inner join 
    table_2 t4 on t4.date = t1.date
               and t4.to = t1.to
  • Related