加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-12-11 23:38:27 所属栏目:MySql教程 来源:网络整理
导读:此代码不起作用 select pagenr into @offset from pages where id = 3;select * from table1 limit @offset*10,10; 我需要使用什么SQLcode才能使这种代码工作 仅使用SQL! 注意 SET SQL_SELECT_LIMIT = @count 不起作用,因为我主要关注的是偏移,而不是限制.

此代码不起作用

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

我需要使用什么SQLcode才能使这种代码工作
仅使用SQL!

注意

SET SQL_SELECT_LIMIT = @count 

不起作用,因为我主要关注的是偏移,而不是限制. 最佳答案 从MySQL 5.5规范:

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

  • Within prepared statements,LIMIT parameters can be specified using ?
    placeholder markers.
  • Within stored programs,LIMIT parameters can be specified using
    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;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读