我有一個郵政編碼表,我想用它的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