使用空间查询的结果更新mysql表

时间:2014-01-04 17:47:06

标签: mysql sql geospatial spatial-query

我正在使用mysql来计算面要素(postsec)中点要素(商店)的数量。

我选择的查询工作正常,但我想用生成的结果更新表postsec。我的查询如下:

select pc_sec, count(*) as count from shops 
join postsec on st_within(shops.SHAPE,postsec.SHAPE) 
where shops.shop_cat > 0 and shop_cat < 10 group by pc_sec

我尝试了很多方法来做到这一点,例如更新postsec设置new_column =([选择查询])但似乎无法找到正确的方法。

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效

update postsec 
inner join 
(
   select pc_sec, count(*) as pseccount 
   from shops 
   join postsec on st_within(shops.SHAPE,postsec.SHAPE) 
   where shops.shop_cat between 1 and 9
   group by pc_sec
) x on x.pc_sec = postsec.id
set count_column = x.pseccount