3

I have to table like this. Now tbl2.transaction_id is from tbl1.id. I wanna to update tbl2.transaction_id with tbl1.id_trans values that transaction_id now pointed to them. Can I do this with just one query? Thanks.

tbl1
+------+----------------+
| id   | id_trans       |
+------+----------------+
|   53 | 531            |
|   54 | 532            |
|   55 | 533            |
|   56 | 554            |
|   57 | 555            |
+------+----------------+


tbl2
+------+----------------+
| id   | transaction_id |
+------+----------------+
|   10 | 53             |
|   11 | 55             |
|   12 | 56             |
+------+----------------+
Karim Pazoki
  • 951
  • 1
  • 13
  • 34
  • 1
    Possible duplicate of [mysql update column with value from another table](http://stackoverflow.com/questions/11709043/mysql-update-column-with-value-from-another-table) – Jens Jun 07 '16 at 06:46

3 Answers3

3

Try Update with join:

UPDATE tbl2 t
INNER JOIN tbl1 s
 ON(t.transaction_id = s.id)
set t.transaction_id = s.id_trans
sagi
  • 40,026
  • 6
  • 59
  • 84
1

Try this,

update tbl2
set tbl2.transaction_id = tbl1.id_trans 
from tbl1 inner join tbl2
on tbl1.id = tbl2.transaction_id
where tbl2.transaction_id = 53;
1

You can use this query:

    UPDATE tbl2
INNER JOIN tbl1 ON tbl1.id = tbl2.transaction_id
SET
    tbl2.transaction_id = tbl1.id_trans
aarju mishra
  • 710
  • 3
  • 10