简体   繁体   中英

update a table using inner join syntax in mysql

i am curious about how updating a table with inner join works. if i run the following statement:

update tbl1 a
inner join tbl2 b using (id)
set a.val = b.val;

what happens to the records in tbl1 that do not have a match in tbl2 ? will they simply not be updated and left as is in tbl1 ? will they be deleted?

i realize i can run this and get the answer but i'm also interested in the mechanics of how this works behind the scenes and was hoping somebody could elucidate this for me.

Q: What happens to the records in tbl1 that do not have a match in tbl2 ?

A: They will not be updated since they have no match from tbl2 . From the definition, the INNER JOIN keyword return rows when there is at least one match in both tables. The only keyword that could delete record from your table is the DELETE DML.

The update statement is operating on tbl1.

The join is providing a filter that specifies which rows to update in tbl1.

As an added bonus, the join is also providing a value for the column.

The update statement does not and cannot delete rows from a table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM