我有一个邮政编码表,我想用它的3个最近的邻居更新每个邮政编码。即填补空白此表:mySQL - 使用select返回多行更新多列
postcode nearestPostcode1 nearestPostcode2 nearestPostcode3
_______________________________________________________________
KY6 1DA - - -
KY6 1DG - - -
KY6 2DT - - -
KY6 1RG - - -
....
我已经想通了一个SELECT查询找到最近的邮政编码,这里是一个笨拙的方式,第一行可能被更新:
update table1 set
nearestPostcode1 = (select query for returning the first nearest postcode),
nearestPostcode2 = (select query for returning the second nearest postcode),
nearestPostcode3 = (select query for returning the third nearest postcode)
where postcode = 'KY6 1DA';
但是,这将导致为每个行更新运行3个选择查询。
update table1 set
(nearestPostcode1, nearestPostcode2, nearestPostcode3) =
(select query to return the 3 nearest postcodes)
where postcode = 'KY6 1DA';
在上面看起来“选择查询”类似这样的:如果有一些方法做的是通过这个伪代码表示这将是更有效的
select postcode from postcodeTable
order by <equation to calculate distance> ASC
limit 3
反正对从select中返回的行将被放入一个可用于更新多个字段的表单中? 谢谢。
“最近的邮政编码”是如何确定的? – Thomas 2011-05-12 22:34:42
@Thomas纬度和经度也存储在邮政编码表中,我转换为米和使用pythageros – spiderplant0 2011-05-12 22:45:56