计算组合表的距离

时间:2014-04-10 10:18:20

标签: mysql sql

我有以下SQL语句:

SELECT Distinct(Venue_Name), Count(*) as `num1` 
FROM venuetagview 
INNER JOIN 
(
SELECT Tag,COUNT(*) AS `num` 
FROM usertags 
WHERE UserID=4
GROUP BY Tag 
ORDER BY `num` DESC
) as a 
ON venuetagview.Tag=a.Tag 
GROUP BY Venue_Name 
ORDER BY `num1` DESC

我正在尝试使用venuetagview表中的纬度/经度为其添加“距离”计算。我试过这个:

SELECT  
  ( 6371 
  * acos( cos( radians(51.529099) ) 
  * cos( radians( Venue_Latitude ) ) 
  * cos( radians( Venue_Longitude ) 
  - radians(-0.084981) ) 
  + sin( radians(51.529099) ) 
  * sin( radians( Venue_Latitude ) ) ) ) AS distance, 
Distinct(Venue_Name), 
Count(*) as `num1` 
FROM venuetagview 
INNER JOIN 
(
SELECT Tag,
COUNT(*) AS `num` 
FROM usertags 
WHERE UserID=4
GROUP BY Tag 
ORDER BY `num` DESC
) as a 
ON venuetagview.Tag=a.Tag 
GROUP BY Venue_Name 
ORDER BY `num1` DESC

但是我在使用它时遇到错误。

2 个答案:

答案 0 :(得分:0)

我发布的查询仅显示

  1. distinct是关键作品,应该像distinct column_name

    一样使用

    SELECT Distinct (Venue_Name)应该是SELECT Venue_NameSELECT Distinct Venue_Name

答案 1 :(得分:0)

如草莓所述,DISTINCT解决了这个问题:

SELECT
( 6371 * acos( cos( radians(51.529099) ) * cos( radians( Venue_Latitude ) ) * cos( radians( Venue_Longitude ) - radians(-0.084981) ) + sin( radians(51.529099) ) * sin( radians( Venue_Latitude ) ) ) ) AS distance, Venue_Name, Count(*) as
{NUM1 {1}} {NUM {1}} {NUM {1}}