MySQL计数不起作用甚至查询返回结果

时间:2018-10-10 01:32:11

标签: mysql

我正在检索此select语句的计数。

SELECT
    count(video_id),
    3959 * acos( cos( radians(51.482600) ) * cos( radians( videos.lat ) ) * cos( radians(videos.lon) - radians(-0.063200)) + sin(radians(51.482600)) * sin( radians(videos.lat))) AS distance
FROM
    videos
WHERE
    rooms >= 1 AND price <= 3500
HAVING
    distance <= 3

它始终显示行结果为空,但是当我删除count(video_id)并使用*时,它会返回结果:

SELECT
    *,
    3959 * acos( cos( radians(51.482600) ) * cos( radians( videos.lat ) ) * cos( radians(videos.lon) - radians(-0.063200)) + sin(radians(51.482600)) * sin( radians(videos.lat))) AS distance
FROM
    videos
WHERE
    rooms >= 1 AND price <= 3500
HAVING
    distance <= 3

如果对该语句感到困惑,则将距离小于或等于3英里的给定位置的经纬度与“视频”表中列的经纬度进行比较,并返回结果。

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用子查询:

SELECT video_id, count(video_id) as num, distance FROM (SELECT video_id ,3959 * acos( cos( radians(51.482600) ) * cos( radians( videos.lat ) ) * cos( radians(videos.lon) - radians(-0.063200)) + sin(radians(51.482600)) * sin( radians(videos.lat))) AS distance FROM videos WHERE rooms >= 1 AND price <= 3500 HAVING distance <= 3) WHERE group by video_id;

SELECT video_id,count(video_id),3959 * acos( cos( radians(51.482600) ) * cos( radians( videos.lat ) ) * cos( radians(videos.lon) - radians(-0.063200)) + sin(radians(51.482600)) * sin( radians(videos.lat))) AS distance FROM videos WHERE rooms >= 1 AND price <= 3500 GROUP BY video_id HAVING distance <= 3

答案 1 :(得分:0)

您需要像这样的子查询来计算行数:

"SELECT count(*) 
 from 
  (SELECT 3959 * acos( cos( radians(51.482600) ) * cos( radians( videos.lat ) ) * cos( radians(videos.lon) - radians(-0.063200)) + sin(radians(51.482600)) * sin( radians(videos.lat))) AS distance 
   FROM videos 
   WHERE rooms >= 1 AND 
         price <= 3500 
   HAVING distance <= 3") as Result;

请注意,您必须给子查询一个别名(我称其为Result)。

我测试了这样的查询,它可以正常工作-希望这可以解决问题