mysql – 如何仅使用(My)SQL使限制偏移动态化

前端之家收集整理的这篇文章主要介绍了mysql – 如何仅使用(My)SQL使限制偏移动态化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码不起作用

select pagenr into @offset from pages where id = 3;
select * from table1 limit @offset*10,10;

我需要使用什么sqlcode才能使这种代码工作@H_502_10@仅使用sql

注意

SET sql_SELECT_LIMIT = @count 

不起作用,因为我主要关注的是偏移,而不是限制.

最佳答案
MysqL 5.5规范:

The LIMIT clause can be used to@H_502_10@ constrain the number of rows returned@H_502_10@ by the SELECT statement. LIMIT takes@H_502_10@ one or two numeric arguments,which@H_502_10@ must both be nonnegative integer@H_502_10@ constants,with these exceptions:

  • Within prepared statements,LIMIT parameters can be specified using ?@H_502_10@ placeholder markers.
  • Within stored programs,LIMIT parameters can be specified using@H_502_10@ integer-valued routine parameters or local variables as of MysqL 5.5.6.

因此,在存储过程中,以下内容将起作用:

DECLARE offset bigint
SELECT pagenr * 10 INTO offset FROM pages where id = 3;
SELECT * FROM table1 LIMIT offset,10;

否则,您需要预先计算该值并通过查询传入.您应该已经知道页面大小和页码,因此这应该不难.

猜你在找的MySQL相关文章