0

Table t1 - Fields a, b Table t2 - Fields a, c

(a is common field between two tables)

I have added the field b in t2 and want to populate data for the same using references in t1

This can be done using following queries (e.g. a = 100, 101, ...)

update t2 set b = (select b from t1 where a = 100 limit 1) where a = 100;

update t2 set b = (select b from t1 where a = 101 limit 1) where a = 101;

Is there way this can be batched?

1 Answer 1

1

Use join:

update t2 join
       t1
       on t2.a = t1.a
    set t2.b = t1.b;

You could also use a correlated subquery:

update t2
    set b = (select t1.b from t1 where t1.a = t2.a);

However, non-matching values would be set to NULL.

Sign up to request clarification or add additional context in comments.

Comments

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.