简体   繁体   English

使用mysql中的内部联接语法更新表

[英]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 ? tbl1中的记录在tbl2没有匹配会tbl2什么? will they simply not be updated and left as is in tbl1 ? 他们根本不会更新并保留在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 ? 问: tbl1tbl2没有匹配的记录会发生什么?

A: They will not be updated since they have no match from tbl2 . 答:他们不会更新,因为他们与tbl2没有匹配。 From the definition, the INNER JOIN keyword return rows when there is at least one match in both tables. 根据定义,当两个表中至少有一个匹配时, INNER JOIN关键字返回行。 The only keyword that could delete record from your table is the DELETE DML. 可以从表中删除记录的唯一关键字是DELETE DML。

The update statement is operating on tbl1. update语句在tbl1上运行。

The join is providing a filter that specifies which rows to update in tbl1. 连接提供了一个过滤器,用于指定要在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. update语句不会也不能从表中删除行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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