高级 MySql 查询:使用另一个表中的信息更新表
我想用另一个表中的数据更新 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意,如果 sort_order 是 INT,则不要使用 '1' - 使用 1:
Note, if sort_order is an INT, then don't use '1' - use 1:
试试这个,对我来说效果很好。
Try this, it works fine for me.