如何在mysql中导出此选择结果

时间:2013-07-17 11:06:48

标签: mysql sql

我有select语句,它将数据作为一个名为new_iata_code

的新虚拟列返回

我想将此数据作为真实列并将其与表格一起导出。如何执行此操作?

    select cni.*,
// this statment return data as new column called  (new_iata_code )
           (select iata_code
            from cities_iata ci
            order by pow(cni.latitude - ci.latitude, 2) + pow(cni.longitude - ci.longitude, 2)
            limit 1
           ) as new_iata_code
// the table name is cities_no_iata cni
    from cities_no_iata cni;

//更新代码不起作用

update cities_no_iata join
       (select cni.*,
               (select iata_code
                from cities_iata ci
                order by pow(cia.latitude - ci.latitude, 2) + pow(cia.longitude - ci.longitude, 2)
                limit 1
               ) as new_iata_code
        from cities_no_iata cni
       ) upd
       on cities_no_iata.latitude = upd.latitude and
          cities_no_iata.longitude = upd.longitude
     set iata_code = upd.iata_code;

1 个答案:

答案 0 :(得分:0)

首先创建TEMPORARY表:

CREATE TEMPORARY TABLE tmp select cni.*,
           (select iata_code
            from cities_iata ci
            order by pow(cni.latitude - ci.latitude, 2) + pow(cni.longitude - ci.longitude, 2)
            limit 1
           ) as new_iata_code
    from cities_no_iata cni

然后立即更新:

UPDATE cities_no_iata c
INNER JOIN tmp t on c.id = t.id //or what ever id column is named
SET c.new_iata_code = t.new_iata_code