简体   繁体   English

从另一个表更新表PostgreSQL

[英]Update table from another table postgresql

I have 2 tables. 我有2张桌子。 Table A contains 3 columns: Month,Code,Point while table B contains 5 column: Code,Point1,Point2,Point3,Point4 . Table A包含3列: Month,Code,Pointtable B包含5列: 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). 我想根据表A的月份(只有4个月分配到第1-4点)更新表B中的积分。

I assume TableA (Code) is a foreign key to TableB (Code), and that TableA (Month,Code) is unique. 我假设TableA(代码)是TableB(代码)的外键,而TableA(月,代码)是唯一的。 I also take from your explanation that month can only be 1,2,3 or 4. 我还从您的解释中得出,月份只能是1,2,3或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),
     (...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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