如何在mysql中基于另一个表更新表? [英] how to update table based on another table in mysql?

查看:66
本文介绍了如何在mysql中基于另一个表更新表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子作为休假:

i have 2 tables as fallow:

users
zip      state      city
89012    
45869     0
....


zips
zip      state      city
89012    NV         lv
45869    MI         ca
....

我想基于users表中的zipzips中的state and city更新users: state and city

i would like to update users: state and city with the state and city from zips based on the zip from the users table in an efficient way

users表中的city为空,但state也可以为0或空

the city in users table is empty but the state can also be 0 or empty

有什么想法吗?

谢谢

推荐答案

您是否要批量更新所有现有行?

Are you trying to update all of the existing rows in bulk?

如果是这样,这是进行更新的一种方法:

If so, here's one way to do the update:

update users u
  inner join zips z on z.zip = u.zip
set u.state = z.state,
  u.city = z.city;

这篇关于如何在mysql中基于另一个表更新表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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