2011-08-29 66 views
4

以下查詢爲我提供了一列距離。 但我需要的只是匹配距離的結果數量,而不是距離本身。 Subselect不能使用。COUNT(*)和

SELECT 
(6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869)))) AS Distance 

FROM ... 

WHERE ... 

HAVING Distance > 0 AND Distance <= 25 
+2

你剛剛在你的問題中忽略了GROUP BY嗎?或者你根本沒有? – Vache

+0

我不在這裏使用一個。 – Mike

回答

2

如果你不需要的距離,只有數,也許這將工作:

SELECT Count(*) 
FROM ... 
WHERE ... AND 
    (6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * 
    (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * 
    cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869))) 
) BETWEEN 0 AND 25 
+0

謝謝!我忘了'Between'運營商。 – Mike

1

這將給出totalResults,並且您可以放棄其他列。

SELECT COUNT(*) totalResults, 
    (6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869)))) AS Distance 

    FROM ... 

    WHERE ... 

    HAVING Distance > 0 AND Distance <= 25 
+0

我當然試過了。但是,我得到了一個空的結果集(即零行)。 – Mike

3

你只需要距離計算移動到WHERE子句:

SELECT COUNT(*) FROM ... 

WHERE (6368 * SQRT(2*(1-...) BETWEEN 0 AND 25