sql查询纬度和经度

本文关键字:经度 纬度 查询 sql | 更新日期: 2023-09-27 18:01:00

我有这个表

CREATE TABLE [dbo].[EnquiryMaster](
    [EnquiryId] [int] IDENTITY(1,1) NOT NULL,
    [orgid] [int] NULL,
    [uid] [int] NULL,
    [Company] [varchar](160) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HeadOfficeAddress] [varchar](460) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ZipCode] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Latitude] [float] NOT NULL,
    [Longitude] [float] NOT NULL,   
    CONSTRAINT [PK_EnquiryMaster] PRIMARY KEY CLUSTERED 
    (
        [EnquiryId] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

我想写一个查询,这样它将显示我将从下拉列表中选择的公司的100度经纬度内的所有公司。

sql查询纬度和经度

试试这个

select * 
from [dbo].[EnquiryMaster] 
where (Latitude <= @lat and Longitude <= @long 
      and Latitude + 100 >= @lat and  Longitude +100 >= @long)
OR (Latitude >= @lat and Longitude >= @long 
      and Latitude - 100 <= @lat and  Longitude -100 <= @long)
OR (Latitude >= @lat and Longitude <= @long 
      and Latitude - 100 <= @lat and  Longitude + 100 >= @long)
OR (Latitude <= @lat and Longitude >= @long 
      and Latitude + 100 >= @lat and  Longitude - 100 <= @long)
where Latitude  between @lat  - 100 and @lat  + 100
and   Longitude between @long - 100 and @long + 100