How to do it in mysql: If id = idold THEN UPDATE status = 1

I would like to compare two tables and then update if some logic is correct,
In pseudocode:

SELECT * FROM users, usersold IF users.id = usersold.id THEN UPDATE users.status = 1;

Is there a way to do this in mysql?

+1


source to share


1 answer


UPDATE users u
SET status = 1
WHERE EXISTS (SELECT id FROM usersold WHERE id = u.id)

      

Alternative version:



UPDATE users
SET status = 1
WHERE id IN (SELECT id FROM usersold)

      

You should test, and depending on your database, you may find it performs better than another, although I expect any decent database to be optimized to be the same anyway.

+5


source







All Articles