如果在其他地方得到回答,我会立即道歉。我似乎无法获得这个版本的工作版本,我尝试了许多不同的东西,一些来自知识,另一些来自谷歌搜索。
我在我的SQL查询中使用Haversine公式来订购几家零售店的距离,以便我可以在地图上绘制它们。我得到的距离和不好的东西,但当我添加一个选项来过滤掉副本或链,商店我遇到了问题。问题根据我尝试的不同查询而有所不同,我想知道是否有人可以指出我哪里出错了所以我可以从这种经验中学习。 :d
我尝试过的事情(注意:这些示例中的lat / lon是假lat / lon而我得到的lat / lon来自另一个SQL表):
SELECT store_id,col_a,col_b,col_c,store_name,store_number,street_address,apt_suite,city,state_id,zip_code,latitude,longitude,phone_number,phone_extension,fax_number,email_addr,location_direction,open_24_hr,website_url, (3959*acos(cos(radians(12.1234567)) * cos(radians(latitude)) * cos(radians(longitude)-radians(-45.678910.)) + sin(radians(12.1234567)) * sin(radians(latitude)))) AS distance FROM stores WHERE primary_provider_code = '01' OR secondary_provider_code = '01' OR tertiary_provider_code = '01' ORDER BY distance LIMIT 0 , 10
这将让我完成基本工作。获取最近的10个商店,按距离排序,只选择我想要的列。现在当有人点击“过滤同一链”选项时,我尝试了以下内容:
SELECT store_id,col_a,col_b,col_c,store_name,store_number,street_address,apt_suite,city,state_id,zip_code,latitude,longitude,phone_number,phone_extension,fax_number,email_addr,location_direction,open_24_hr,website_url, (3959*acos(cos(radians(12.1234567)) * cos(radians(latitude)) * cos(radians(longitude)-radians(-45.678910.)) + sin(radians(12.1234567)) * sin(radians(latitude)))) AS distance FROM stores WHERE primary_provider_code = '01' OR secondary_provider_code = '01' OR tertiary_provider_code = '01' GROUP BY store_name ORDER BY distance LIMIT 0 , 10
这确实会渲染结果,但它会逐字地筛选出每个相同的链,而不仅仅是在显示一个链后。例如,如果我们有Walgreens,好市多,沃尔玛,Walgreens,Target等......那么我只想展示第一个Walgreens并过滤第二个。谷歌搜索让我相信GROUP BY子句会起作用。它删除了所有Walgreens。实际上,它删除了所有具有重复项的行。
为了尝试,我还尝试了GROUP BY store_name HAVING COUNT(*) = 1
和>
,<
,>=
,<=
版本。
我不相信我可以做SELECT DISTINCT
,因为其他列本身都是不同的,所以即使store_name可能有重复项,我试图选择所有其他行很独特。此外,我尝试了这一点,并确认它不会过滤掉非唯一列。
提前感谢您的帮助。请注意,我已经标记了PHP,因为已知它与SQL接口,而我正在开发的平台是PHP。
注意:我不是只找一家商店。我正在寻找所有商店,按距离排序,分组并过滤掉重复的商店。假设未经过滤的结果是
Wal-Mart
K-Mart
Wal-Mart
Walgreens
Costco
Sams Club
Wal-Mart
Costco
Walgreens
我想返回以下过滤结果:
Wal-Mart
K-Mart
Walgreens
Costco
Sams Club
答案 0 :(得分:1)
一种解决方案: -
SELECT store_id,col_a,col_b,col_c,stores.store_name,store_number,street_address,apt_suite,city,state_id,zip_code,latitude,longitude,phone_number,phone_extension,fax_number,email_addr,location_direction,open_24_hr,website_url, sub1.distance AS distance
FROM stores
INNER JOIN
(
SELECT store_name, MIN(3959*acos(cos(radians(12.1234567)) * cos(radians(latitude)) * cos(radians(longitude)-radians(-45.678910.)) + sin(radians(12.1234567)) * sin(radians(latitude)))) AS distance
FROM stores
WHERE primary_provider_code = '01' OR secondary_provider_code = '01' OR tertiary_provider_code = '01'
GROUP BY store_name
) sub1
ON stores.store_name = sub1.store_name
AND (3959*acos(cos(radians(12.1234567)) * cos(radians(latitude)) * cos(radians(longitude)-radians(-45.678910.)) + sin(radians(12.1234567)) * sin(radians(latitude)))) = sub1.distance
WHERE primary_provider_code = '01' OR secondary_provider_code = '01' OR tertiary_provider_code = '01'
ORDER BY distance LIMIT 0 , 10
这使用子查询获取每个链的最近存储(因此在子查询中为MIN / GROUP BY),然后将其连接到stores表以获取最近存储的完整详细信息。
答案 1 :(得分:0)
我无法在您的SQL中看到错误,我在我的数据库中测试它并获得正确的结果。
SELECT store_id,
col_a,
col_b,
col_c,
store_name,
store_number,
street_address,
apt_suite,
city,
state_id,
zip_code,
latitude,
longitude,
phone_number,
phone_extension,
fax_number,
email_addr,
location_direction,
open_24_hr,
website_url,
(3959*acos(cos(radians(12.1234567)) * cos(radians(latitude)) * cos(radians(longitude)-radians(-45.678910.)) + sin(radians(12.1234567)) * sin(radians(latitude)))) AS distance
FROM stores
WHERE
primary_provider_code = '01' OR
secondary_provider_code = '01' OR
tertiary_provider_code = '01'
GROUP BY GROUP BY store_name
ORDER BY distance LIMIT 0 , 10
Alternativ试试这个:
SELECT store_id,
col_a,
col_b,
col_c,
DISTINCT(store_name),
store_number,
street_address,
apt_suite,
city,
state_id,
zip_code,
latitude,
longitude,
phone_number,
phone_extension,
fax_number,
email_addr,
location_direction,
open_24_hr,
website_url,
(3959*acos(cos(radians(12.1234567)) * cos(radians(latitude)) * cos(radians(longitude)-radians(-45.678910.)) + sin(radians(12.1234567)) * sin(radians(latitude)))) AS distance
FROM stores
WHERE
primary_provider_code = '01' OR
secondary_provider_code = '01' OR
tertiary_provider_code = '01'
ORDER BY distance LIMIT 0 , 10
答案 2 :(得分:0)
如果您只想要最近商店的不同商店名称,那么您可以在当前查询的基础上进行DISTINCT,如下所示:
SELECT DISTINCT STORE_NAME
FROM
(SELECT
STORE_ID,
COL_A,
COL_B,
COL_C,
STORE_NAME,
STORE_NUMBER,
STREET_ADDRESS,
APT_SUITE,
CITY,
STATE_ID,
ZIP_CODE,
LATITUDE,
LONGITUDE,
PHONE_NUMBER,
PHONE_EXTENSION,
FAX_NUMBER,
EMAIL_ADDR,
LOCATION_DIRECTION,
OPEN_24_HR,
WEBSITE_URL,
(3959 *ACOS(COS(RADIANS(12.1234567)) * COS(RADIANS(LATITUDE)) * COS(RADIANS(
LONGITUDE)-RADIANS(-45.678910.)) + SIN(RADIANS(12.1234567)) * SIN(RADIANS(
LATITUDE)))) AS DISTANCE
FROM
STORES
WHERE
PRIMARY_PROVIDER_CODE = '01'
OR SECONDARY_PROVIDER_CODE = '01'
OR TERTIARY_PROVIDER_CODE = '01'
ORDER BY
DISTANCE LIMIT 0 ,
10
) nearest_stores;