按存储过程MYSQL中调用的函数返回的值排序

时间:2012-08-27 10:31:31

标签: mysql stored-procedures stored-functions

我想通过dist订购: dist 是一个名为 isNeighbour 的函数返回的double值因为未定义dist而抛出错误:字段列表中的未知列'dist'

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 ;

1 个答案:

答案 0 :(得分: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;
相关问题