高级 MySql 查询:使用另一个表中的信息更新表

2021-11-20 00:00:00 sql-update mysql

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

I would like to update a table in mySql with data from another table.

我有两个表people"和business".人员表通过名为business_id"的列链接到业务表.

I have two tables "people" and "business". The people table is linked to the business table by a column called "business_id".

必要的表结构,主键加星号(表:列):人员:*business_id、*sort_order、电子邮件业务:*business_id,电子邮件

The necessary table structure, primary key is starred (Table: columns): People: *business_id, *sort_order, email Business: *business_id, email

我想用来自 people 表的电子邮件更新业务表的电子邮件列,类似这样(我知道我在这里遗漏了一些东西):

I would like to update the business table email column with the email from the people table, something like this (I know I am missing something here):

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

这有意义吗?可能吗?

推荐答案

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

相关文章