Samul Samul - 3 years ago 196
MySQL Question

Mysql query does not look right

I want to update a field in my table based in another table and I executed this query below but I think it's not right.. it looks like it worked but is it correct? Is there any situation where it might fail?

UPDATE users SET page = (SELECT page_name FROM pages WHERE user_id = id)


My table USERS has a column id and page. My table PAGES has a column page_name and user_id. Is the code above right?

Answer Source

Instead of subselect In mysql you can use UPDATE JOIN

  UPDATE users 
  INNER JOIN pages on pages.user_id = users.id
  SET users.page = pages.page_name
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download