Search code examples
postgresqlsql-updatejsonb

update table based on JSONB columns joins


I'm trying to update my table and the code that isn't working is

UPDATE A
   SET person_id = B.id 
  FROM B
 WHERE A.info.EmpNumber = B.info.EmpNumber

both the info columns are of type jsonb

A.info looks like this

{
  "position": "Data Engineer",
  "EmpNumber": "382159"
}

B.info looks like this

{
  "salary": 80000,
  "EmpNumber": "382159"
}

Could you point me in the right direction?

The error is

ERROR:  missing FROM-clause entry for table A
LINE 4: WHERE

Solution

  • You can just use ->> operator in order to extract the value from the related key such as

    UPDATE A
       SET person_id = B.id 
      FROM B
     WHERE A.info->> 'EmpNumber' = B.info->> 'EmpNumber'
    

    Demo