如何在 MySql 的 limit 子句中使用乘法

How to use multiplication in limit clause in MySql

我想在请求中将 LIMIT 级别的两个数字相乘,但它给了我一个错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''POSITIONS' LIMIT (5-1),10' at line 1 

请求如下:

SELECT * FROM 'POSITIONS' LIMIT (5*1),10

查看 MySQL 的文档,似乎 Limit 必须是一个常量值。

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).

这会限制您将数学运算作为值的一部分。准备好的语句异常将允许您使用一个参数,您之前在其中进行了计算并使用一个整数参数来表示限制,但不确定这是否是一个选项。

在 MySQL 版本 5.5.6 之前,您唯一的选择是使用准备好的语句:

SET @skip=1; SET @numrows=5;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';
EXECUTE STMT USING @skip, @numrows;

在版本 5.5.6 之后,documentation 指出: "Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6."