sql函数与最接近排序之间的距离

发布于 2024-10-11 01:28:55 字数 306 浏览 2 评论 0原文

我使用此功能来获取 30 英里内的事件,它与书呆子晚餐的功能几乎相同。但是我怎样才能按“最接近”来排序......?

ALTER FUNCTION [dbo].[NearestEvents] 
  ( 
  @lat real, 
  @long real 
  ) 
RETURNS  TABLE 
AS 
  RETURN 
  SELECT Events.ID 
  FROM   Events  
  WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 

Im using this function to get events within 30 miles, its pretty much the same as the nerd dinner one. But How can I order them by 'closest'..?

ALTER FUNCTION [dbo].[NearestEvents] 
  ( 
  @lat real, 
  @long real 
  ) 
RETURNS  TABLE 
AS 
  RETURN 
  SELECT Events.ID 
  FROM   Events  
  WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2024-10-18 01:28:55

将其添加到您的选择列表中。

SELECT Events.ID,
  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) as distance
  FROM   Events  
  WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 
  ORDER BY distance

--由于它仍然不适用于 OP,调试存储过程的一个好方法是劫持参数。使用查询分析器尝试一下,看看您会得到:

DECLARE @lat = varchar(100);
DECLARE @long = vavhar(100);

SET @lat = 'XXXXX';
SET @long = 'XXXXX';

 SELECT Events.ID,
      dbo.DistanceBetween(@lat, @long, Latitude, Longitude) as distance
      FROM   Events  
      WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 
      ORDER BY distance;

Add it to your SELECT list.

SELECT Events.ID,
  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) as distance
  FROM   Events  
  WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 
  ORDER BY distance

--Since it is still not working for OP a good way to debug stored procedure is by hijacking the parameters. Try this with your Query Analyser and see that you get:

DECLARE @lat = varchar(100);
DECLARE @long = vavhar(100);

SET @lat = 'XXXXX';
SET @long = 'XXXXX';

 SELECT Events.ID,
      dbo.DistanceBetween(@lat, @long, Latitude, Longitude) as distance
      FROM   Events  
      WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 
      ORDER BY distance;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文