gpt4 book ai didi

php - 纠正 MySQL 空间查询的逻辑

转载 作者:行者123 更新时间:2023-11-29 12:50:38 28 4
gpt4 key购买 nike

我的主要目标是在 MySQL 中创建一个PROCEDURE,以根据传递的纬度和经度查询位置。该查询获取传递给 PROCEDURE 的特定半径内位置的 ID、纬度和经度。我还尝试添加一个 JOIN 来查询“照片”表中每个位置的照片。

查询部分有效。它需要返回所有没有 double 的位置,以及字符串中的所有照片。

GROUP_CONCAT(CONVERT(photos.pid, CHAR(8))) AS photo

添加这一 block 代码后,查询仅返回 1 个结果,并将所有照片 ID 连接到一个字段中,即使它与该位置无关。如果我删除这段代码,它会返回每个位置 1,但对于具有 1 张或多张与之关联的照片的位置,结果将加倍,因此需要连接照片字段,但我没有正确执行此操作。

CREATE PROCEDURE `GEODIST`( IN userid int, IN dist int, IN olat float, IN olon float ) DETERMINISTIC READS SQL DATA

BEGIN
DECLARE mylon DOUBLE;
DECLARE mylat DOUBLE;
DECLARE lon1 FLOAT;
DECLARE lon2 FLOAT;
DECLARE lat1 FLOAT;
DECLARE lat2 FLOAT;

SET mylon = olon;
SET mylat = olat;
SET lon1 = mylon - dist / abs( cos( radians( mylat ) ) * 69 );
SET lon2 = mylon + dist / abs( cos( radians( mylat ) ) * 69 );
SET lat1 = mylat - ( dist / 69 );
SET lat2 = mylat + ( dist / 69 );

SELECT GROUP_CONCAT(CONVERT(photos.pid, CHAR(8))) AS photo, destination.id,
destination.latitude, destination.longitude,
3956 * 2 * ASIN(SQRT( POWER(SIN((origin.latitude -destination.latitude) * pi()/180 / 2), 2)
+COS(origin.latitude * pi()/180) * COS(destination.latitude * pi()/180)
*POWER(SIN((origin.longitude -destination.longitude) * pi()/180 / 2), 2) ))
AS distance FROM locations destination CROSS JOIN locations origin LEFT JOIN photos ON photos.lid = destination.id WHERE origin.id = userid
AND destination.longitude BETWEEN lon1 AND lon2 AND destination.latitude BETWEEN lat1 and lat2
HAVING distance < dist ORDER BY distance LIMIT 50;
END

最佳答案

我认为它应该像这样,但我无法测试它。

SELECT 
GROUP_CONCAT( CONVERT( DISTINCT( photos.pid ), CHAR(8) ) ) ) AS photo,
destination.id,
destination.latitude,
destination.longitude,
3956 * 2 * ASIN(SQRT( POWER(SIN((origin.latitude -destination.latitude) * pi()/180 / 2), 2)
+COS(origin.latitude * pi()/180) * COS(destination.latitude * pi()/180)
*POWER(SIN((origin.longitude -destination.longitude) * pi()/180 / 2), 2) ))
AS distance
FROM
locations destination
CROSS JOIN
locations origin
LEFT JOIN
photos ON photos.lid = destination.id
WHERE
origin.id = userid
AND
destination.longitude BETWEEN lon1 AND lon2
AND destination.latitude BETWEEN lat1 and lat2
HAVING distance < dist
GROUP BY
destination.id
ORDER BY
distance
LIMIT 50;

关于php - 纠正 MySQL 空间查询的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com