当字段包含第二个表的索引时,从另一个表更新表

时间:2012-05-19 23:58:28

标签: sql sql-update

The case:

我有一个表(300行)房地产这些细节:

     id     | locality| postcode| declat  | declong | alias             |
    ---------------------------------------------------------------------    
     13     |         |         |         |         | bla bla wimar bla |
     14     |         |         |         |         | bla bla bla bla   |
     15     |         |         |         |         | bla bla bla arafo |

和其他包含此数据的邮政编码表(900行):

    locality| postcode | lat        | long
    ---------------------------------------------    
      cande | 504      | 37.9418594 | -0.7795244
      arafo | 501      | 38.9418594 | -0.7795255  
      wimar | 500      | 39.9418594 | -0.7795277

我需要像这样更新表格的行:

      id     | locality| postcode| declat     | declong   | alias             |
    ---------------------------------------------------------------------------    
      13     |  wimar  | 500     | 39.9418594 |-0.7795277 | bla bla wimar bla |
      14     |         |         |            |           | bla bla bla bla   |
      15     |  arafo  | 501     | 38.9418594 |-0.7795255 | bla bla bla arafo |

谢谢。

1 个答案:

答案 0 :(得分:0)

根据您的rdbms,如果这只是一次性的,您可以尝试这样的事情:

UPDATE realty
  SET locality = p.locality
     ,postcode = p.postcode
     ,declat =  p.declat
     ,declong = p.declong
  FROM postcodes p
  WHERE alias LIKE '%' || p.locality || '%'

要注意的一堆东西(扫描,重复匹配等)