2012-08-27 50 views
1

我想通过DIST命令:DIST是从名为isNeighbour函数返回一个双值DIST未定义它抛出错误:在未知列“DIST”字段列表排序从函数返回的值称为存储过程MYSQL

DELIMITER $$ 

DROP PROCEDURE IF EXISTS `connectarabs`.`maxEdges` $$ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `maxEdges`(id int,lat double,lon double,rad double) 
BEGIN 


if lat is null then 

set lat=(select a.latitude from account a where a.id=id); 
end if; 
if lon is null then 
set lon=(select a.longitude from account a where a.id=id); 
end if; 
SELECT friends.* FROM account friends left join account_friendTest me on (friends.id=me.second_account) 
    or (friends.id=me.first_account) where (me.first_account=id OR me.second_account=id) AND friends.id <> id AND 
    ( ((select isNeighbour(lat,lon,friends.latitude,friends.longitude,rad) as dist)<rad) ) order by dist; 
END $$ 
DELIMITER ; 
+0

使用'HAVING'条款代替。 – Omesh

回答

1

这是因为你不能在WHERE子句中使用和使用,在ORDER BY条款别名列。相反,你需要SELECT该列,并使用HAVING条款对其进行过滤:

SELECT friends.*, 
     isNeighbour(lat,lon,friends.latitude,friends.longitude,rad) AS dist 
FROM account friends 
    LEFT JOIN account_friendTest me 
     ON (friends.id=me.second_account) 
      OR (friends.id=me.first_account) 
WHERE (me.first_account=id OR me.second_account=id) AND 
     friends.id <> id 
HAVING dist < rad 
ORDER BY dist; 
+0

是的,这是工作非常感谢非常多,但为什么我必须把dist YouYou

+0

我已经将该条件从'WHERE'子句转移到'HAVING'条款。如果你将这个条件放在WHERE子句中,那么它必须执行两次函数计算,这会降低查询的性能。 – Omesh

+0

不客气! :)不要忘记接受最好的答案。见:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Omesh