I altered my table structure today and created the 'Country' fields/table and I need to perform an UPDATE query to bring everything in line.
I have thousands of records. CityID has been populated with the IDs from the City lookup table since the database was created. CountryID is currently empty as a new field.
I have begun to associate each CityID with a CountryID. For example, New York City's CityID = "25" and the USA's CountryID = "1", so these values are inputted into the City table.
I would like to UPDATE Main.CountryID retrospectively, so for any records where Main.CityID = "25", it should automatically insert Main.CountryID = 1
Does that make sense?
My Main table:
MainID
Name
Address etc.
CityID
CountryID (Empty)
My City table
CityID
City
CountryID
My Country table
CountryID
Country
Apologies for protracted question, it's been a long day...
Do an join update of the Main
table with the City
table:
UPDATE Main a
INNER JOIN City b
ON a.CityID = b.CityID
SET a.CountryID = b.CountryID
As you have values in City Table update the Main table column on the basis of join with City Table
Try this, i hope this works fine.
Update t1
set t1.CountryID = t2.CountryID
FROM Main AS t1
INNER JOIN City AS t2 ON t1.CityID = t2.CityID
Try this one. I already try it.
UPDATE mainid a JOIN city b
ON a.cityID = b.cityID
SET a.countryID = b.countryID
Okay, I will explain it. if You wanna get countryID based on cityID. You should join table MainID with table cityID because in that table there is cityID and countryID . So, join mainid a JOIN city b a.cityID = b.cityID
and set a.countryID = b.countryID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.