0

cont表包含许多相同的记录userid。我正在尝试从dist列中为每个唯一用户 ID 选择最小距离值。

SELECT sec.*, r.contid, r.lng, r.lat, **math formula for distance** < $withinMiles AS dist
FROM cont r
JOIN s_cont s
ON r.userid = s.userid
WHERE (( **math formula for distance**))
ORDER BY dist ASC
4

1 回答 1

1

您需要使用带有子句的分组:

SELECT sec.*, r.contid, r.lng, r.lat, MIN(**math formula for distance**) AS dist
FROM cont r
JOIN s_cont s
ON r.userid = s.userid
GROUP BY r.userid
HAVING MIN(**math formula for distance**) < $withinMiles
ORDER BY dist ASC

GROUP BY 将具有相同用户标识的行组合在一起,MIN() 将从这些行中提取最小值(即具有相同用户标识)。HAVING 就像 WHERE 一样工作,但在已经聚合的值上。

于 2013-10-23T15:23:47.170 回答