I've read a post about how to randomly select a row (with certain conditions) from a SQL table with huge data. They get the max and min of id and generate a random number between them, and get the first row that has a bigger id than that. However, my ids are not distributed evenly, so I didn't get the truly random row. For example if my ids are 1, 100, 101, I'll have little chance to get the two later rows.
But I think of another solution. Instead of getting the max id, I count all the row in my query, get a random number i and select the ith one. The code looks like this
$count_res = $mysqli->query("SELECT COUNT(*) FROM quest WHERE category IN ({$mem['my_cate']})");
$count = $count_res->fetch_array();
$rand_id = rand(0, $count[0] - 1);
$result = $mysqli->query("SELECT * FROM quest WHERE category IN ({$mem['my_cate']}) LIMIT 1 OFFSET $rand_id");
However, i doubt it's effectiveness. Can anyone give me idea about that, or suggest a solution for my case. Thanks.