简体   繁体   中英

Insert using selecting column from other table

I have three tables as below

三张表的预期结果

I want to insert the column dep_typ in table 2 as selecting the column from table 1.

But the dep_typ in table 2 has all 4 values as 'U' and whereas in table 1 it is three times 'U' and 1 time 'F'. I want the result to be as same as table 1.

The following will join the dep_type values from t1 with only the records from t2. Your data sample does not have any matching ex_line values common to t1 and t2. Is this true of the entire dataset? It if isn't true for the entire dataset, then @OldProgrammer is right - the join will not return the exact number of records in t1.

SELECT
    t1.position,
    t1.ss_id,
    t2.ex_line,
    t1.dep_Typ,
    t1.num
FROM
    t1
       INNER JOIN
          t2
             ON t1.position = t2.position
             AND t1.ss_id = t2.ss_id
                INNER JOIN
                    t3
                         ON t1.ss_id = t3.ss_id
                         AND t1.value = t3.value
                         AND t2.vech_num = t3.vech_num
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM