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

发布于 2024-07-29 18:41:17 字数 418 浏览 3 评论 0原文

我想用另一个表中的数据更新 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 = ''; 

这有意义吗? 是否可以?

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

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

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

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

Does this make sense? Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2024-08-05 18:41:17
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 = ''
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 = ''
假面具 2024-08-05 18:41:17

注意,如果 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 = '';

Note, if sort_order is an INT, then don't use '1' - use 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 = '';
z祗昰~ 2024-08-05 18:41:17

试试这个,对我来说效果很好。

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

Try this, it works fine for me.

Update table a, table b
Set a.importantField = b.importantField,
a.importantField2 = b.importantField2
where a.matchedfield = b.matchedfield;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文