高级MySQL查询:从另一个表信息更新表(Advanced MySql Query: Update

2019-06-17 19:16发布

我想在MySQL中更新表与另一个表中的数据。

我有两个表“人”与“业务”。 百姓餐桌是由一个名为“business_id”栏链接到业务表。

必要的表结构,主键出演(表:列):人:* business_id,* SORT_ORDER,电子邮件业务:* business_id,电子邮件

我想更新与百姓餐桌的电子邮件业务表电子邮件列,这样的事(我知道我在这里的东西):

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

这是否有意义? 可能吗?

Answer 1:

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 = ''


Answer 2:

请注意,如果排序顺序为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 = '';


Answer 3:

试试这个,它工作正常,我。

Update table a, table b
Set a.importantField = b.importantField,
a.importantField2 = b.importantField2
where a.matchedfield = b.matchedfield;


文章来源: Advanced MySql Query: Update table with info from another table