2013-07-10 54 views
0

我有3個表如何更新通過連接兩個表在MySQL

  1. iata_en表3列(CITY_NAME,COUNTRY_NAME,iata_code)
  2. d_cities表3列(CITY_NAME,COUNTRY_ID,iata_code)
  3. d_country表2的列(COUNTRY_ID,COUNTRY_NAME)

我想iata_code柱從表複製到iata_en表d_citites其中(iata_en.cityname = d_cities.cityname)和iata_en.countryname = d_cities.country_nam e)

  • 所以我加入了d_cities和d_country與外鍵得到 country_name。

我寫這篇文章的代碼,但它不工作

UPDATE d_cities 
     SET iata_code=iata_en.iata_code 
     FROM iata_en,d_cities as ci 
     INNER JOIN d_country as co 
     ON ci.CountryID=co.CountryID 
     WHERE iata_en.city_name=ci.city 
     AND iata_en.country_name=co.country 

回答

2

所有表引用必須將UPDATE子句中,如果你指定甚至實際上並沒有更新它們。

UPDATE 
    d_cities 
    JOIN d_country USING (CountryID) 
    JOIN iata_en ON (
     city_name = city 
     AND country_name = country 
    ) 
SET 
    d_cities.iata_code = iata_en.iata_code 
0

嘗試此查詢:

UPDATE D_CITIES CITIES 
JOIN D_COUNTRY COUNTRY ON COUNTRY.COUNTRY_ID = CITIES.COUNTRY_ID 
JOIN IATA_EN IATA ON IATA.CITY_NAME = CITIES.CITY_NAME 
SET CITIES.IATA_CODE = IATA.IATA_CODE; 
+0

沒有什麼不是我want..i要確保國家名稱和城市名稱等於其在傳輸之前iata_en城市和國家名稱iata_code..so我想加入副本之前d_cities和d_country數據 – user1673473

+0

編輯查詢。 –