我正在處理一個卡車運輸問題,其中有多個分銷中心(DC)和多個商店。我對區議會和商店都有經度和緯度。我不知道如何循環47個不同的DC經線和緯度以及4500多個商店的經度和緯度,然後找到每個DC的最短距離。我想是這樣的(但我認爲,臨時表是錯誤的):使用循環在SQL中查找多個距離
CREATE TEMPORARY TABLE tempStores AS (SELECT * FROM wm_stores)
SET i = 0;
WHILE i <= (SELECT COUNT(*) FROM tempStores) - 1 DO
SELECT store_id FROM tempStores LIMIT i,1 INTO thisStoreID;
SELECT store_id FROM tempStores LIMIT i,1;
SET i = i + 1;
END WHILE;
DECLARE storeLat DOUBLE;
DECLARE storeLon DOUBLE;
DECLARE dcLat DOUBLE;
DECLARE dcLon DOUBLE;
DECLARE storeLatRad DOUBLE;
DECLARE storeLonRad DOUBLE;
DECLARE dcLatRad DOUBLE;
DECLARE dcLonRad DOUBLE;
DECLARE R DOUBLE;
DECLARE distance DOUBLE;
SET R = 3961;
SET storeLat = 37.350659; ## <-- Basically Loop this here
SET storeLon = -76.734855; ## <-- Here
SET dcLat = 37.192498; ## <-- Here
SET dcLon = -77.534201; ## <-- And here... instead of having it all hard coded.
SET storeLatRad = PI() * storeLat/180.0;
SET storeLonRad = PI() * storeLon/180.0;
SET dcLatRad = PI() * dcLat/180.0;
SET dcLonRad = PI() * dcLon/180.0;