D.R.
D.R.

Reputation: 1219

MySQL UPDATE statement

I have 2 tables, calls and accounts. Individual "calls" have parent "account" and both tables have an "assigned user" column so that calls and accounts can be assigned to specific people. In the process of out database cleanup, we want to set the assigned user of each call to the assigned user of the corresponding account. Unfortunately we don't have a test database to work with and I wanted to run this update statement by everyone here to make sure it would not have an undesired results. My only fear is that the SET statement would perhaps set the same user for ALL of the calls instead of pulling the assigned user for each one. Here it is:

UPDATE 
    FROM calls AS c
    INNER JOIN accounts AS a ON c.parent_id=a.id
SET c.assigned_user_id=a.assigned_user_id
WHERE c.status='PLANNED'

c.status is simply a flag to only change future ('PLANNED') calls not the ones in the past

My fear is that if I have Account A assigned to X and Account B assigned to Y and Account C assigned to Z that when the update executes, they'll all get assigned to Z instead of the corresponding account user. Am I correct or just overthinking this? Essentially, are updates handled as one big batch or is each row evaluated individually?

Upvotes: 0

Views: 318

Answers (1)

geekosaur
geekosaur

Reputation: 61467

The rows are joined the same way they are in SELECT, so each c will be matched with its corresponding a.

Upvotes: 1

Related Questions