mysql 存储过程 select 变量,MySQL SELECT语句中的存储过程变量

I'm trying to create a stored procedure. Here's what I have so far (not working):

DELIMITER |

CREATE PROCEDURE getNearestCities(IN cityID INT)

BEGIN

DECLARE cityLat FLOAT;

DECLARE cityLng FLOAT;

SET cityLat = SELECT cities.lat FROM cities WHERE cities.id = cityID;

SET cityLng = SELECT cities.lng FROM cities WHERE cities.id = cityID;

SELECT *, HAVERSINE(cityLat,cityLng, cities.lat, cities.lng) AS dist FROM cities ORDER BY dist LIMIT 10;

END |

HAVERSINE is a function I created which works fine. As you can see I'm trying to take the id of a city from the cities table and then set cityLat and cityLng to some other values of that record. I'm obviously doing this wrong here by using SELECTs.

Is this even possible. It seems it should be. Any help whatsoever will be greatly appreciated.

解决方案

Corrected a few things and added an alternative select - delete as appropriate.

DELIMITER |

CREATE PROCEDURE getNearestCities

(

IN p_cityID INT -- should this be int unsigned ?

)

BEGIN

DECLARE cityLat FLOAT; -- should these be decimals ?

DECLARE cityLng FLOAT;

-- method 1

SELECT lat,lng into cityLat, cityLng FROM cities WHERE cities.cityID = p_cityID;

SELECT

b.*,

HAVERSINE(cityLat,cityLng, b.lat, b.lng) AS dist

FROM

cities b

ORDER BY

dist

LIMIT 10;

-- method 2

SELECT

b.*,

HAVERSINE(a.lat, a.lng, b.lat, b.lng) AS dist

FROM

cities AS a

JOIN cities AS b on a.cityID = p_cityID

ORDER BY

dist

LIMIT 10;

END |

delimiter ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值