Skip to content
Advertisement

How to update each entry of a table for each entry of another table in MySQL

I try to update a table by replacing text in it by a mapping table. The solution I came up with works, but only for the one entry.

How can I update all entries of item for each entry in the id_mapping table?

Example tables:

item

id type text
1 1 <span><a href="item_id=111">Link</a></span>
2 1 <span><a href="item_id=222">Link</a></span>
3 1 <span><a href="item_id=333">Link</a></span>
4 2 <span><a href="item_id=444">Link</a></span>

id_mapping

old_id new_id
111 999
222 888
333 777
444 666

The update query I came up with that should update all entries, but actually only updates the first entry:

After execution of the update it changed to:

item

id type text
1 1 <span><a href="item_id=999">Link</a></span>
2 1 <span><a href="item_id=222">Link</a></span>
3 1 <span><a href="item_id=333">Link</a></span>
4 2 <span><a href="item_id=444">Link</a></span>

What I expected:

item

id type text
1 1 <span><a href="item_id=999">Link</a></span>
2 1 <span><a href="item_id=888">Link</a></span>
3 1 <span><a href="item_id=777">Link</a></span>
4 2 <span><a href="item_id=444">Link</a></span>

Am I doing something wrong with the basic concept of this?

Advertisement

Answer

You need to join tables in a way. For example

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement