2014-04-24 105 views
0

我的直接道歉,如果這已在其他地方回答。我似乎無法得到這樣的工作版本,並且我嘗試了許多不同的東西,其中一些來自知識,另一些來自Google的搜索。從MySQL表中過濾重複的行

我在我的SQL查詢中使用Haversine公式來訂購一些零售商店的距離,以便我可以在地圖上繪製它們。我得到的距離和什麼不好,但是當我添加一個選項來過濾掉重複或連鎖店,我遇到了問題。這些問題根據我嘗試的不同查詢而有所不同,我想知道是否有人可以指出我要出錯的地方,以便我可以借鑑此經驗。 :d

事我已經試過(注:經/緯度在這些例子都是假的緯度/經度和緯度/經度我得到的是從另一個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 

這不渲染的結果,但它確實過濾掉所有的同鏈,一個是顯示不只是後所有。例如,如果我們有沃爾格林,好市多,沃爾瑪,沃爾格林,塔吉特等......那麼我只想展示第一批沃爾格林,並過濾掉第二批沃爾格林。谷歌搜索引導我相信GROUP BY條款將起作用。它刪除了所有的Walgreens。實際上,它刪除了所有重複的行。

我自己也嘗試GROUP BY store_name HAVING COUNT(*) = 1><>=<=變化的嘗試着想。

我不相信我能做到,因爲其他列都將是對自己不同的,所以即使STORE_NAME可能有重複的,所有我想選擇其他行是唯一一個SELECT DISTINCT。此外,我已經嘗試了這一點,並確認它不會過濾掉非獨特的列。

在此先感謝您的幫助。請注意,我也標記了PHP,因爲它已知與SQL接口,而我正在開發的平臺是PHP。

注:我不是隻尋找一家商店。我正在尋找所有商店,按距離排序,分組和過濾出重複商店。比方說,未經過濾的結果是

Wal-Mart 
K-Mart 
Wal-Mart 
Walgreens 
Costco 
Sams Club 
Wal-Mart 
Costco 
Walgreens 

我想回到的濾波的結果:

​​
+0

一個GROUP BY將刪除重複。問題是,如果你有(說)10個沃爾瑪商店和你的GROUP BY連鎖店名稱,它將帶回沃爾瑪商店之一。哪一個(即,不在GROUP BY中的所有字段的內容)未定義。 – Kickstart

回答

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中子查詢),然後將其與商店表加入以獲取最近商店的完整詳細信息。

+0

工作!我很感激這個幫助,這是一個很好的學習經歷,重新寫了一下,以便輸入我需要的額外部分。 – lxndr

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 

吳國良試試這個:

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 
+0

我不只是在尋找一家商店。我正在尋找該地區的所有商店,篩選出額外的商店。 – lxndr

+0

我也不能。但是,當我執行「GROUP BY」子句時,我將失去所有在數據庫中具有額外或重複的商店。所以,因爲我沒有sql錯誤和不正確的結果,所以我想問問SO的人們。 – lxndr

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;