yKim
yKim

Reputation: 63

multiple row update with one SQL

I have two tables below

t1

-------------------------------------
| id | MaleCnt | FemaleCnt | flag |
------------------------------------
  1       20       null        1
  2       30       null        1
  3       40       null        1

t2

----------------------------
| id | FemaleCnt | flag |
----------------------------
  1       20        1
  2       30        1
  3       40        1

I want to update "FemaleCnt" at table t1 with table t2 (shoud have same id and flag)


I just wrote some query but dont work, so far.

Could you give me some tip??

Upvotes: 0

Views: 57

Answers (2)

DJo
DJo

Reputation: 2177

This should work.

UPDATE t1 SET t1.FemaleCnt = t2.FemaleCnt 
WHERE t1.id = t2.id AND t1.flag = t2.flag 

Upvotes: 1

Yogesh Sharma
Yogesh Sharma

Reputation: 50173

Just do JOIN & update :

UPDATE t1 INNER JOIN
    t2
    ON t2.id = t1.id AND t2.flag = t1.flag
    SET t1.FemaleCnt = t2.FemaleCnt; 

Upvotes: 2

Related Questions