我有一个SQL Server 2012位置表,其中有一个geography
类型的列geo
我有一个链接到位置表的用户表
位置
locationId int
geo geography
用户
userId int
locationId int
我会知道位置的id,并且知道如何使用stdistance进行距离查询。但是,在查询中比较两个距离的最佳方法是什么。我可以用子选择来做到这一点,但有没有更好的方法
子选择看起来像
select
suburb, state, postcode,
geo.STDistance((select geo from location where locationId = 47))/1000 kms
from location
order by kms desc
位置 #47 是否特别?会永远是47吗?无论哪种方式,您都可以将其粘贴到变量中
declare @HQ geography;
select @HQ = geo from location where locationid = 47;
select
suburb, state, postcode,
geo.STDistance(@HQ)/1000 kms
from location
order by kms desc
如果您(无论出于何种原因)希望在一个查询中完成所有内容,则可以尝试外部应用程序
select
suburb, state, postcode,
geo.STDistance(HQ.geo)/1000 kms
from location
outer apply (select geo from location where locationid = 47) as HQ
order by kms desc