41

我想用另一个表中的数据更新 mySql 中的一个表。

我有两个表“人”和“业务”。人员表通过名为“business_id”的列链接到业务表。

必要的表结构,主键加星号(表:列):人员:*business_id,*sort_order,email 业务:*business_id,email

我想用来自人员表的电子邮件更新业务表电子邮件列,如下所示(我知道我在这里遗漏了一些东西):

UPDATE business b SET email = (SELECT email  from People p where p.business_id = b.business_id AND sort_order = '1') WHERE b.email = ''; 

这有意义吗?是否可以?

4

3 回答 3

115
UPDATE business b, people p
   SET b.email = p.email
 WHERE b.business_id = p.business_id
   AND p.sort_order = '1'
   AND b.email = ''
于 2009-07-29T18:19:19.423 回答
16

注意,如果 sort_order 是一个 INT,那么不要使用 '1' - 使用 1:

UPDATE business b
JOIN People p
ON p.business_id = b.business_id
AND p.sort_order = '1'
SET b.email = p.email
WHERE b.email = '';
于 2009-07-29T19:34:51.613 回答
0

试试这个,它对我来说很好用。

Update table a, table b
Set a.importantField = b.importantField,
a.importantField2 = b.importantField2
where a.matchedfield = b.matchedfield;
于 2018-06-01T17:59:58.420 回答