我可以在 LIMIT 偏移量中使用 MySQL 函数吗
Can I use MySQL functions in the LIMIT offset
我可以在 LIMIT
偏移量中使用 MySQL 函数吗?
喜欢:
SELECT * FROM sites WHERE ... LIMIT FLOOR(1 + RAND() * (SELECT COUNT(*) FROM sites)) , 1
不,你不能直接。 LIMIT
和 OFFSET
值必须是常量。
引用自the MySQL docs:
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).
不过您可以使用准备好的语句和变量:
SELECT @offset:=FLOOR(1 + RAND() * COUNT(*)) FROM sites;
PREPARE STMT FROM 'SELECT * FROM sites WHERE ... LIMIT ?, 1';
EXECUTE STMT USING @offset;
我可以在 LIMIT
偏移量中使用 MySQL 函数吗?
喜欢:
SELECT * FROM sites WHERE ... LIMIT FLOOR(1 + RAND() * (SELECT COUNT(*) FROM sites)) , 1
不,你不能直接。 LIMIT
和 OFFSET
值必须是常量。
引用自the MySQL docs:
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).
不过您可以使用准备好的语句和变量:
SELECT @offset:=FLOOR(1 + RAND() * COUNT(*)) FROM sites;
PREPARE STMT FROM 'SELECT * FROM sites WHERE ... LIMIT ?, 1';
EXECUTE STMT USING @offset;