日期:2014-05-16 浏览次数:20801 次
CREATE FUNCTION getTargetFloor(message VARCHAR(255),appID INT,tID INT) RETURNS INT BEGIN DECLARE floor INT; DECLARE id INT; SET floor = 0; SET id = 0; SELECT `id` INTO id FROM `k`.`comment` WHERE `tid`=tID AND `appid`=appID ORDER BY `id` LIMIT floor,1; END IF; RETURN(id); END
mysql> use test; Database changed mysql> delimiter $$ mysql> drop procedure if exists getId$$ Query OK, 0 rows affected (0.00 sec) mysql> create procedure getId(in appID int,in tID int,in lim int) -> begin -> set @sql=concat('select id into @iid from comment WHERE appid= ',appID,' AND tid= ',tID,' ORDER BY id LIMIT ',lim,',1'); -> PREPARE stmt from @sql; -> EXECUTE stmt; -> end ;$$ Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> CALL getID(25,3399,0); Query OK, 0 rows affected (0.00 sec) mysql> SELECT @iid; +--------+ | @iid | +--------+ | 144000 | +--------+ 1 row in set (0.00 sec) mysql>
------解决方案--------------------
DELIMITER $$ CREATE FUNCTION getTargetFloor(message VARCHAR(255),appID INT,tID INT) RETURNS INT BEGIN DECLARE `floor` INT; DECLARE Aid INT; SET `floor` = 0; SET Aid = 0; SELECT `id` INTO Aid FROM `k`.`comment` WHERE `tid`=tID AND `appid`=appID ORDER BY `id` LIMIT FLOOR,1; RETURN Aid; END$$ DELIMITER ;