首页 > 解决方案 > 从另一个表中的数据更新一个表中的数据

问题描述

我的表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)

我在表中创建了此countrylocations以更新表中的countryCustomers。喜欢:

UPDATE locations SET country = (SELECT country FROM customers);

但上面的查询给出了错误:Subquery returns more than 1 row

我怎么做?

标签: mysqlsql

解决方案


我认为您需要 UPDATE 使用 JOIN 语法。尝试这个 -

UPDATE locations L
JOIN Customers C ON C.city = L.city
SET L.COUNTRY = C.COUNTRY;

推荐阅读