Mysql – Copy data from one table into other on some condition

MySQL

I have two table as follows.

Table : city

city_id  city_name   state_id
  1       Cachar       1
  2      Darrang       1
  3       Nicobar      1 

Table 2 : locality

pincode  Address       city
110020   abc           Nicobar
110021   abcd          Cachar
110024   abcd          Cachar
110023   abc           Nicobar

in table city i have 1430 rows , containing different cities in India.
And in table locality each city contains near about 100 pincode .

What i want to do ?
i want to replace each city(column) in table : locality with its corresponding city_id in table : city .

Question : How can i do this ? Is there any fast way to this ? I don't want to use any programming language ie. php ,java . Is there any procedure , looping in Mysql ?

Result looks like ie.

Table 2 : locality

pincode  Address       city
110020   abc             3
110021   abcd            1
110024   abcd            1
110023   abc             3

Best Answer

ALTER TABLE locality ADD COLUMN city_id ...;  -- add new column
UPDATE locality
  JOIN city  ON city.city_name = locality.city
   SET locality.city_id = city.city_id;  -- set new column
ALTER TABLE locality DROP COLUMN city;   -- cleanup by removing old column