user2138513
user2138513

Reputation: 19

How do i Update data in table from other table with condition

i have 2 tables and both has one column as a primary and foreign key. i have to update one table column which is empty but the primary table has the values which needs to be updated here. how do i update that particular column by referencing those columns of primary table in foreign-key table?

Table 1 - Primary -Column list SI.No (PrimaryKey) UpdateTime StudentDetail

table 2 - Foreign - Column list SI.No(ForeignKey) UpdateTime BatchCode

Table 2 of updateTime is empty for some students due to some reason. I need to get the update time from table 1 of those empty students and update it to table 2. how do i do this?? using postgress i am.

Upvotes: 0

Views: 59

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

In Postgres, you can use a FROM clause to reference another table:

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

Upvotes: 1

Related Questions