0

我有 3 张桌子

  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_name)

  • 所以我用外键加入了 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
4

2 回答 2

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
于 2013-07-10T11:48:41.070 回答
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;
于 2013-07-10T11:48:04.600 回答