PHP How to get a random value like in rand ($ min, $ max) but with a query?

1

For example, in a browser game I want the user to click on attack to return another user who has + - the same battle power as he.

$select = $mysqli->query("select * from data order by rand()");

This way it always returns a random value, but what I want is a value between x and y ... something like rand ($ min, $ max), but from what I saw, order by rand does not work that way ...

Does anyone give me a light how to do this?

    
asked by anonymous 06.06.2016 / 04:28

1 answer

1

Where [min] is the minimum number and [max] the maximum.

$select = $mysqli->query("select * from data order by rand() LIMIT [max], [min]");

This query is slow because LIMIT will do a sort, an interesting link on random record selection performance: link

An alternative, where id is a single numeric key:

$select = $mysqli->query("select * from data where id between [min] and [max] order by rand()");
    
06.06.2016 / 06:16