cont
表包含许多具有相同userid
的记录。我正在尝试为每个唯一用户ID从dist
列中选择最小距离值。
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
答案 0 :(得分: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会将具有相同用户ID的行组合在一起,MIN()将从这些行中提取最小值(即使用相同的用户ID)。 HAVING就像WHERE一样工作,但是已经聚合了值。