MySQL COUNT(*)返回空结果集

时间:2014-12-03 15:45:44

标签: php mysql

我目前正在MySQL中的表上执行大圆距离计算。该表如下所示:

+----------+---------+---------+
| venue_id | lat     | lng     |
+----------+---------+---------+
| 1        | 1.23456 | 2.34567 |
+----------+---------+---------+
| 2        | 1.23456 | 2.34567 |
+----------+---------+---------+
| 3        | 1.23456 | 2.34567 |
+----------+---------+---------+

但是,当我尝试创建返回结果的COUNT()时,MySQL返回一个空结果集。

例如,以下查询不会产生任何结果:

SELECT COUNT(*) AS total, 
       ( 3959 * Acos(Cos(Radians('52.97682200')) * Cos(Radians(lat)) * Cos( 
                                   Radians(lng) - Radians(-0.02210000)) + 
                              Sin(Radians(52.97682200)) * Sin(Radians(lat))) ) 
       AS distance 
FROM   wv_venue_locations 
HAVING distance < 5
ORDER  BY distance

然而,在没有COUNT(*) AS total字段的情况下运行相同的查询会产生预期的6个结果。

任何人都可以建议为什么会这样,以及我如何解决它?

作为上述的后脚本,以下工作正常:

SELECT Count(*) 
       AS 
       total, 
       ( 3959 * Acos(Cos(Radians(53.18335000)) * Cos(Radians(lat)) * Cos( 
                                   Radians(lng) - Radians(-0.29600000)) + 
                              Sin(Radians(53.18335000)) * Sin(Radians(lat))) ) 
       AS 
       distance 
FROM   wv_venue_locations 
WHERE  lat >= 52.64017900
       AND lat <= 53.72650900
       AND lng >= -0.94998000
       AND lng <= 0.35798000

以上正确输出:

+-------+-------------------+
| total | distance          |
+-------+-------------------+
| 224   | 27.93840157954865 |
+-------+-------------------+

4 个答案:

答案 0 :(得分:2)

要计算行数,您可以执行此操作

SELECT SQL_CALC_FOUND_ROWS 
       ( 3959 * Acos(Cos(Radians('52.97682200')) * Cos(Radians(lat)) * Cos( 
                                   Radians(lng) - Radians(-0.02210000)) + 
                              Sin(Radians(52.97682200)) * Sin(Radians(lat))) ) 
       AS distance 
FROM   wv_venue_locations 
HAVING distance < 5
ORDER  BY distance

选择了所需的行后,您可以通过以下单个查询获取计数:

SELECT FOUND_ROWS();

答案 1 :(得分:0)

测试了它,它似乎返回你想要的。如果这不正确,请告诉我

SELECT SUM(1) as total,
       ( 3959 
        * Acos(  Cos(Radians('52.97682200'))
               * Cos(Radians(lat)) 
               * Cos(Radians(lng) - Radians(-0.02210000)) 
               + Sin(Radians(52.97682200)) 
               * Sin(Radians(lat))
              ) 
       ) AS distance 
FROM   wv_venue_locations 
GROUP BY distance
HAVING distance < 5
ORDER  BY distance

FIDDLE DEMO

主要问题是没有group by和AGGREGATE函数,它会将所有行组合成一个...你需要告诉它将它们分开。所以我用距离说,因为你得到一个纬度和长度的距离列表。

编辑:

如果您想要返回一行,显示距离为5的总位置以及您可以执行此类操作的总距离。

SELECT SUM(1) as total, SUM(distance) as total_distance, MAX(distance) as furthest_location, MIN(distance) as closest_location
FROM
(   SELECT
        ( 3959 
            * Acos(  Cos(Radians('52.97682200'))
                   * Cos(Radians(lat)) 
                   * Cos(Radians(lng) - Radians(-0.02210000)) 
                   + Sin(Radians(52.97682200)) 
                   * Sin(Radians(lat))
                  ) 
           ) AS distance 
    FROM   wv_venue_locations 
    HAVING distance < 5
    ORDER  BY distance
) t

<强> SECOND DEMO

答案 2 :(得分:0)

我相信我弄清楚为什么使用HAVING不会返回任何结果,因为HAVING会在分组后过滤行。由于您要对整个表进行分组,因此只返回一行。然后,它将按HAVING进行过滤。

这是更清晰的图片,

SELECT COUNT(*) AS total, (....) AS distance
FROM wv_venue_locations

此查询将返回一行。假设行是

total, distance
100, 6.432

然后,应用HAVING distance < 5。没有符合此条件的行


我不知道这是否是OP想要的

SELECT COUNT(*), MIN(distance)
FROM (
  SELECT 
       ( 3959 * Acos(Cos(Radians('52.97682200')) * Cos(Radians(lat)) * Cos( 
                                   Radians(lng) - Radians(-0.02210000)) + 
                              Sin(Radians(52.97682200)) * Sin(Radians(lat))) ) 
       AS distance 
   FROM   wv_venue_locations
) AS t
WHERE distance < 5

答案 3 :(得分:-2)

您的distance < 5应该处于WHERE条件,而不是HAVING条件。如果您只想显示HAVING输出符合特定条件的结果,则会使用COUNT()