0

Though my sql knowledge is pretty good, I cannot get my head around the difference in a left vs inner join specifically when doing an update.

employee_table
column1:id
column2:socialsecurity


private_info_table
column1:id
column2:socialsecurity

I need to update employee_table.socialsecurity to be private_info_table.socialsecurity

should I do a left or inner join: ???

update e
set e.socialsecurity=p.socialsecurity
from employee_table e
join private_info_table p  --should this be left or inner join?
on p.id=e.id
4

2 回答 2

3

左连接

Select *
from A 
left join B on a.id = b.pid

在条件a.id = b.pid中,A 和 B 的每一行都返回,但是如果 A (ieaid) 中的值与 B (即 B) 中的值不匹配,则 B 的所有字段都将为空。另一方面,对于条件为真的那些行,将显示 B 的所有值。请注意,必须返回 A 的值,因为它位于“ left ”关键字的左侧。

内部联接

Select *
from A 
inner join B on a.id = b.pid

对于 a.id = b.pid 为 true 的行,将返回行,否则如果为 false,则不返回任何行。这是一个互斥连接。

在您的情况下,不要使用左连接,因为“左”关键字左侧的所有记录都将使用空值或非空值进行更新。这意味着您将根据我对左连接的描述无意中使用空值更新不匹配的记录。

于 2013-08-05T17:42:55.610 回答
1

It should be an inner join if you just want to update those records that are in both tables. If you do a left join it will update the employee table to be null wherever the id isn't found in the private info table.

The reason this matter is if you have some social security numbers in the employee table that already that aren't found in the private info table. You wouldn't want to erase them with a null value.

于 2013-08-05T17:37:10.743 回答