What does using a negative value for MySQL LIMIT do?

泄露秘密 提交于 2020-01-03 10:19:09

问题


So what happens when you use a negative value in a LIMIT clause? Will there be negative consequences (slight pun intended)?

I don't see any documentation on this, and MySQL forums, as I recall, sux.

Background: I'm using MySQL CE 5.5.20. I wrote a stored procedure that take an int parameter and uses it for the LIMIT clause. I needed a way to return all rows if I wanted, so I tried passing in -1 for the limit parameter in my store procedure/routine, and that worked.


回答1:


According to the documentation, it must be a non-negative integer

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 (except when using prepared statements).

It sounds to me like what you are experiencing is an undocumented bug, in that mysql should throw an error if the parameters are not inline with what the documentation requires.

My suggestion would be to use syntactically corrected SQL in your stored procedure. You could still use a negative int as a flag to show all though. Something like this

IF myparam >= 0 THEN
    SELECT * FROM `mytable` LIMIT myparam;
ELSE
    SELECT * FROM `mytable`;
END IF;



回答2:


When using this on other versions of MySQL, it might generate an error.



来源:https://stackoverflow.com/questions/9852956/what-does-using-a-negative-value-for-mysql-limit-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!