检索指定距离内的记录mysql

时间:2013-05-27 18:06:43

标签: mysql

运行此查询时出现以下错误:

SELECT count(*) AS COUNT,
       ((ACOS(SIN(
        (SELECT latitude
         FROM cities
         WHERE name = 'london' & country = 'gb')*PI()/180) * 
         SIN((cities.latitude)*PI()/180)+COS(                                                                                                      
 (SELECT latitude
  FROM cities
  WHERE name = 'london' && country = 'gb')*PI()/180)*COS((cities.latitude)*PI()/180)*COS((
(SELECT longitude
 FROM cities
 WHERE name = 'london' && country = 'gb')-
(cities.longitude))*PI()/180))*180/PI())*60*1.1515) AS distance
FROM tbl_products
LEFT JOIN members ON tbl_products.seller = members.id
LEFT JOIN business ON tbl_products.seller = business.bid
INNER JOIN cars ON tbl_products.prod_id = cars.prod_id
LEFT JOIN car ON cars.car = car.id
LEFT JOIN cities ON members.location = cities.id
LEFT JOIN regions ON cities.region = regions.code && cities.country = regions.country
WHERE tbl_products.title LIKE '%|mazda%' ESCAPE '|'
OR tbl_products.description LIKE '%|mazda%' ESCAPE '|' && regions.country = 'gb' HAVING distance <= '30';

子查询返回超过1行。

我只是想知道如何在给定距离内检索记录。

1 个答案:

答案 0 :(得分:0)

错误表明它的内容。您正在执行子查询,但它会在只需要一行的上下文中返回多行

SELECT count*(*) AS count, ACOS(SIN(...subselect here ...))

很难说你正在做的子选择是什么造成了这个,但你必须找到哪一个,并让它只发回一行。而且由于你在进一步的函数调用中使用了结果,你还必须确保只返回一个FIELD,例如。

SELECT sin(select 1,2 from dual) from dual

会导致相关错误。