简体   繁体   中英

Update table from another table postgresql

I have 2 tables. Table A contains 3 columns: Month,Code,Point while table B contains 5 column: Code,Point1,Point2,Point3,Point4 .

I want to update the point in table B based on months of table A (only 4 months which are allocated into points 1-4).

I assume TableA (Code) is a foreign key to TableB (Code), and that TableA (Month,Code) is unique. I also take from your explanation that month can only be 1,2,3 or 4.

UPDATE tableB b SET
     point1 =
     (SELECT Point FROM TableA
      WHERE Month = 1
      AND Code = b.Code),
     point2 =
     (SELECT Point FROM TableA
      WHERE Month = 2
      AND Code = b.Code),
     (...)

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