Stephen
Stephen

Reputation: 8218

Update table with data from a second table

I have one table with the following columns:

ID, title

I need to update every title cell in this table with the title value from another table that has this structure:

ID, attribute, value

The problem being that table 2 above could have any number of attribute types (title, location, url), and I only want the title attribute copied. I have tried the following but it fails:

UPDATE table1
SET table1.title = table2.value
where table2.attribute='title' and table1.ID = table2.ID;

Any ideas? Thanks in advance.

Upvotes: 0

Views: 36

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

Use join

UPDATE table1 t
JOIN table2 t2 ON t.ID = t2.ID
SET t.title = t2.value
WHERE t2.attribute='title';

Upvotes: 2

Related Questions