How to copy one column of a table into another table's column in PostgreSQL comparing same ID

前端 未结 4 641
南旧
南旧 2020-12-30 01:34

I need to copy ref_id1 from table1 TO the column ref_id2 in the table2 the two things matching will be : id (same column name), a_ref1 & b_ref1 (column names are differe

相关标签:
4条回答
  • 2020-12-30 01:45
    UPDATE public.clean_trips_byobu
    SET trip_dist = clean_trips.bktp_mt_total
    FROM public.clean_trips 
    WHERE public.clean_trips.obu_id = clean_trips_byobu.obu_id
    AND clean_trips.bktp_trip_id = clean_trips_byobu.trip_id;
    

    Hope it will work for you.

    0 讨论(0)
  • 2020-12-30 01:45

    I think this should work:

    UPDATE Table2
    SET ref_id2 = ref_id1
    FROM Table2 
       JOIN Table1 ON 
           Table2.Id = Table1.Id AND Table2.a_ref1 = Table1.b_ref1
    
    0 讨论(0)
  • 2020-12-30 01:56
    UPDATE Table2 --format schema.table_name
    SET 
    ref_id2 = table1.ref_id1
    FROM table1 -- mention schema name
    WHERE table1.id = table2.id
    AND 
    table1.a_ref1 = table2.b_ref1;
    
    0 讨论(0)
  • 2020-12-30 01:56

    What you want is

    UPDATE Table2
    SET ref_id2 = table1.ref_id1
    FROM table1
    WHERE table1.id = table2.id
    AND table1.a_ref1 = table2.b_ref1;
    

    Edit This is what you actually want

    As seen here (crudely)

    0 讨论(0)
提交回复
热议问题