在MySQL的LIMIT子句中使用变量 [英] Using variable in a LIMIT clause in MySQL

查看:820
本文介绍了在MySQL的LIMIT子句中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个存储过程,其中有一个名为 my_size 的输入参数,它是一个 INTEGER .我希望能够在SELECT语句的LIMIT子句中使用它.显然不支持此功能,有没有办法解决此问题?

I am writing a stored procedure where I have an input parameter called my_size that is an INTEGER. I want to be able to use it in a LIMIT clause in a SELECT statement. Apparently this is not supported, is there a way to work around this?

# I want something like:
SELECT * FROM some_table LIMIT my_size;

# Instead of hardcoding a permanent limit:
SELECT * FROM some_table LIMIT 100;

推荐答案

出现了搜索本文.我已经在下面粘贴了相关文本.

A search turned up this article. I've pasted the relevant text below.

这是一个论坛帖子,其中显示了准备好的语句的示例 您将一个变量值分配给limit子句:

Here's a forum post showing an example of prepared statements letting you assign a variable value to the limit clause:

http://forums.mysql.com/read. php?98,126379,133966#msg-133966

但是,我认为此bug应该引起注意,因为我不能 想象过程中准备好的语句将允许任何 过程编译时优化.我有种准备的感觉 语句在过程运行时进行编译和执行, 哪个概率会对效率产生负面影响.如果极限 子句可以接受正常的过程变量(例如,一个过程 参数),那么数据库仍可以执行编译时 在过程中对其余查询进行优化.这 可能会更快地执行该过程.我不是专家 但是.

However, I think this bug should get some attention because I can't imagine that prepared statements within a procedure will allow for any procedure-compile-time optimizations. I have a feeling that prepared statements are compiled and executed at the runtime of the procedure, which probaby has a negative impact on efficiency. If the limit clause could accept normal procedure variables (say, a procedure argument), then the database could still perform compile-time optimizations on the rest of the query, within the procedure. This would likely yield faster execution of the procedure. I'm no expert though.

这篇关于在MySQL的LIMIT子句中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆