如何在有条件的情况下从其他表更新表中的数据

时间:2019-11-25 16:11:34

标签: sql postgresql

我有2个表,并且都具有作为主键和外键的一列。我必须更新一个表列,该列为空,但主表具有需要在此处更新的值。如何通过引用外键表中主表的那些列来更新该特定列?

表1-主列列表 SI号(PrimaryKey) 更新时间 StudentDetail

表2-外部-列列表 SI.No(外键) 更新时间 BatchCode

由于某些原因,某些学生的updateTime表2为空。我需要从空学生的表1中获取更新时间,并将其更新到表2中。我该怎么做?我使用postgress。

1 个答案:

答案 0 :(得分:1)

在Postgres中,您可以使用FROM子句引用另一个表:

update table2 t2
    set updatetime = t1.updatetime
    from table1 t1
    where t1.sl_no = t2.sl_no and t2.updatetime is null;