高级 MySql 查询:使用另一个表中的信息更新表 [英] Advanced MySql Query: Update table with info from another table

查看:44
本文介绍了高级 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 = ''

这篇关于高级 MySql 查询:使用另一个表中的信息更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆