mysql 存储过程中 limit之后使用分页变量,传入分页参数. 在有的时候,我们需要自己手动来控制分页的变量,例如是10条分页,还是20条分页,这个时候limit之后的数字就是变量了
SET ssql="SELECT g.*,? as nextCursor FROM tmp_table_GetTeachnew g LIMIT ?,?";
SET @query=p_Next_Cursor;
SET @fvar=limitCount;
SET @svar=pageSize;
SET @SQUERY=ssql;
PREPARE STMT FROM @SQUERY;
EXECUTE STMT USING @query,@fvar,@svar;
这里不仅仅是 limit之后 使用了2个变量,而且在 from 之前 也使用了一个变量。
类似于微博游标分批次获取信息
有的时候,一次性拿100条数据是很夸张的(相对于安卓手机客户端来说),那么我们就需要分页一下,类似于一个游标,一次取10条或者20条数据,如果还有多的数据,则客户端点击更多,继续获取10条信息
DELIMITER $$
USE `data`$$
DROP PROCEDURE IF EXISTS `fn_json_GetTeachnew`$$
CREATE DEFINER=`root`@`%` PROCEDURE `fn_json_GetTeachnew`(
p_Aid VARCHAR(64),
p_Cursor INT,
p_Newtype VARCHAR(64))
BEGIN
DECLARE ssql VARCHAR(100);
DECLARE pageSize INT;
DECLARE limitCount INT;
DECLARE tempCount INT;
DECLARE p_Next_Cursor VARCHAR(64);
SET pageSize=20;
SET p_Next_Cursor='';
SET limitCount=p_Cursor*pageSize;
DROP TEMPORARY TABLE IF EXISTS tmp_table_GetTeachnew;
CREATE TEMPORARY TABLE tmp_table_GetTeachnew
SELECT `NewId`,`Title`,`Content`,`PicUrl`,`NewTypeTitle` AS NewType,`PublishDate`,`IsHot`
FROM `tb_fdt_new_teachnew` t INNER JOIN `tb_fdt_new_newtype` n
WHERE t.`AgentId`=p_Aid
AND t.`NewType`=n.`NewType`
AND n.`NewTypeTitle`=p_Newtype
ORDER BY PublishDate DESC;
SET tempCount=(SELECT COUNT(*) FROM tmp_table_GetTeachnew); -- 统计看有多少新闻,然后看看你这次获取了之后,下次还能不能获取,如果下次能获取,返回数字,不能获取返回空
IF(tempCount>p_Cursor*pageSize+pageSize) THEN
SET p_Next_Cursor=p_Cursor+1;
END IF;
SET ssql="SELECT g.*,? as nextCursor FROM tmp_table_GetTeachnew g LIMIT ?,?";
SET @query=p_Next_Cursor;
SET @fvar=limitCount;
SET @svar=pageSize;
SET @SQUERY=ssql;
PREPARE STMT FROM @SQUERY;
EXECUTE STMT USING @query,@fvar,@svar;
END$$
DELIMITER ;
最后的一个例子
DELIMITER $$ USE `xiaoxin`$$ DROP PROCEDURE IF EXISTS `J_F_Getnoticemsg`$$ CREATE DEFINER=`root`@`%` PROCEDURE `J_F_Getnoticemsg`( p_uid BIGINT, p_mid BIGINT, -- 上次获取的最小消息ID值 p_functionid INT, p_getType INT, -- 0:获取已发通知,1获取已收通知 p_pagesize INT ) BEGIN DECLARE tempSql VARCHAR(5000); -- 2.5【消息】下拉刷新获取历史通知接口 -- “Mid”: [数字]消息ID, -- “Content”:”消息内容”, //JSON -- “Functionid”:”功能ID”, -- “Senddate”:”发送时间戳”, //按照时间升序,最旧的最上面,最新的最下面 -- “Senderid”: [数字]发送者身份ID, -- “Sendername”:”发送者姓名” -- “ReceiveIds”:”SId,Cid混合的串””//当GetType为0这里有值,当为1 这里没值 IF(p_getType=0) THEN -- 当是获取已发信息的时候 “ReceiveIds” 有值 SET tempSql=' select `Messageid` as `Mid`,`Content`,`Functionid`,`Sendtime`,`Senderid`,`Sendername`,`Targetids` as ReceiveIds from `tb_notice_send` where `Functionid`=? and `Senderid`=? and `Messageid`<? order by `Sendtime` desc limit ?'; SET @fun=p_functionid; SET @t_uid=p_uid; SET @t_mid=p_mid; SET @pagesize=p_pagesize; SET @SQUERY=tempSql; PREPARE STMT FROM @SQUERY; EXECUTE STMT USING @fun,@t_uid,@t_mid,@pagesize; ELSE SET tempSql=' select `Recordid` as `Mid`,`Content`,`Functionid`,`Sendtime`,`Senderid`,`Sendername`,'''' as ReceiveIds from `tb_notice_receive` where `Functionid`=? and `Receiverid`=? and `Recordid`<? order by `Sendtime` desc limit ?'; SET @fun=p_functionid; SET @t_uid=p_uid; SET @t_mid=p_mid; SET @pagesize=p_pagesize; SET @SQUERY=tempSql; PREPARE STMT FROM @SQUERY; EXECUTE STMT USING @fun,@t_uid,@t_mid,@pagesize; END IF; END$$ DELIMITER ;