1

I can SELECT a random number between 0 and 1000 by doing:

SELECT ROUND((RAND()*1000))
-- 931

However, why I try and do that in a limit I get a error:

...LIMIT ROUND((RAND()*1000))

Undeclared variable: round

Why does this error occur? Is these another way to do a random limit like this in mysql?

David542
  • 104,438
  • 178
  • 489
  • 842
  • What mysql version? – danblack Mar 20 '20 at 05:10
  • @danblack 5.7 -- – David542 Mar 20 '20 at 05:11
  • 2
    The parameters to `LIMIT` have to be constants, they can't be expressions. – Barmar Mar 20 '20 at 05:15
  • You may generate a dynamic query, that way you can place the randomly generated limit in the query as a constant. – user2332849 Mar 20 '20 at 05:21
  • @Barmar Not constant. Literal. See prepared statement - LIMIT/OFFSET may be a parameter. – Akina Mar 20 '20 at 05:27
  • @Barmar thanks for that. Out of curiosity, why does mysql implement that design-wise? Do most other DB systems do the same? – David542 Mar 21 '20 at 01:29
  • @David542 `LIMIT` is not a standard SQL feature -- every DBMS has different ways to limit the number of returned records (e.g. SQL-Server uses `SELECT TOP n ...`). I don't know whether the others require the value to be a literal. But MySQL has lots of silly restrictions like this. – Barmar Mar 21 '20 at 17:59

0 Answers0