mysql - 从另一个表中的数据更新一个表中的数据
问题描述
我的表Customers
有 3 列:
ID (primary) city country
1 Chicago USA
2 Chicago USA
3 New York USA
4 Paris France
其他表locations
有 2 列:
city (primary) country
Chicago Null (Empty)
New York Null (Empty)
Paris Null (Empty)
我在表中创建了此country
列locations
以更新表中的country
值Customers
。喜欢:
UPDATE locations SET country = (SELECT country FROM customers);
但上面的查询给出了错误:Subquery returns more than 1 row
我怎么做?
解决方案
我认为您需要 UPDATE 使用 JOIN 语法。尝试这个 -
UPDATE locations L
JOIN Customers C ON C.city = L.city
SET L.COUNTRY = C.COUNTRY;
推荐阅读
- python - Elasticsearch:字段相对于其他字段的分布
- r - R Project - 如何改变控制台文本的颜色?
- google-cloud-platform - 在 Google Compute Engine 上将 Debian 8 升级到 9 的正确方法是什么?
- javascript - 如何在nodejs中重复执行
- python - Pandas遍历行,将列值与列表中的字符串进行比较,从另一列返回值
- python - 表单验证评估为 False Flask
- c++ - 警告隐式转换丢失整数精度
- react-native - 如何在 React-Native 的折线图中绘制 2 个数据
- python - 从数组中提取非“-1”索引的最快方法是什么?
- python - 如何从许多项目的缓冲区中产生单个项目并定期重新填充缓冲区?