简体   繁体   English

从一个sql表搜索并插入到另一个

[英]search from one sql table and insert into another

I have a table called places which has 2 million records. 我有一个称为场所的表,该表具有200万条记录。 In these records there is a column called city_name . 在这些记录中,有一列称为city_name

In another table I have 2 columns: city_name and city_id . 在另一个表中,我有2列: city_namecity_id

My aim is to search the table cities with the city_name from the table places and insert the corresponding city_id from the table cities into the table places. 我的目的是从表位置搜索带有city_name的表城市,并将表城市中的相应city_id插入表位置。

I have tried to use the following: 我尝试使用以下方法:

UPDATE places
INNER JOIN cities USING (city_name)
SET places.city_id = cities.city_id

The problem with this is that it has worked but i am having random city_id's in the city_id field that does not match the city name. 问题是它已经起作用了,但是我在city_id字段中有与城市名称不匹配的随机city_id。

I should also mention that there might not be a corresponding city name in the table cities, so i want to ignore records that do not exist. 我还应该提及的是,表格城市中可能没有相应的城市名称,因此我想忽略不存在的记录。

Can anyone help please. 谁能帮忙。 I am using phpmyadmin 我正在使用phpmyadmin

Thank you 谢谢

If I understand your request correctly the solution would be 如果我正确理解您的要求,解决方案将是

UPDATE places
SET places.city_id = (SELECT cities.city_id
                      FROM cities
                      WHERE cities.city_name = places.city_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM