0

I have two tables with almost 13,000 records and looks something like this

TableA:

ID  Status  Option
----------------------
1 |  Approved  |
2 |  Reject    |
3 |  Approved  |
4
.
.
13,000

TableB

 Name    Option                      Status
-----------------------------------------------
 First  | {'data':'Add into box','ID':'1'}  |  Approved 
 Second | {'data':'Don't Add','ID':'2'}     |  Reject
 Third  | {'data':'Add into box','ID':'3'}  |  Approved 
.
.
.
13,000

I want to fill the Option column (data type varchar)in table A with similar data to that of Table B Option column (data type B) based on same ID which is also in option json object. How do i fill them in one go rather than going one by one.

1
  • Your using a RDBMS, so just define the relationship. It TableB add column status_id then update status_id, create a FK to TableA, finally drop status column from TableB. Commented Nov 11, 2022 at 18:23

1 Answer 1

1

An update query where we set the "option" in TableA using a subquery, where we filter the result based on "id" of TableA matching with "id" inside varchar column "option" of TableB.

update tablea 
set option = (select option from tableb 
              where tablea.id::text = tableb.option::json ->> 'id' 
              limit 1);    
-- assuming id has a 1:1 relation in both tables
Sign up to request clarification or add additional context in comments.

4 Comments

i think my previous explanation of question was not correct.I have updated the table structure.Can you help in that
pls check now. Have updated the access of id from column to varchar json as per your changes.
yes it has worked,i was trying to do something with joins but your method of adding limit 1 in the end is much easier
hi is there any way this query can be optimized because it takes lots of time when i have close to 100,000 records

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.