Pick random result from query. |
[eluser]Fantass[/eluser]
Hello, heres my query Code: $this->db->having('active', 1); I've tried with Mysql this query Code: SELECT * FROM `anuncios` HAVING `active` = 1 AND `counter` < `assigned` AND `widerange` = 2 ORDER BY RAND() limit 1 then i've try adding the line Code: $this->db->order_by("id", "RAND"); Someone could help me? THANKS.
[eluser]Kamarg[/eluser]
You probably need parens after RAND in your order by.
[eluser]CroNiX[/eluser]
http://www.paperplanes.de/2008/4/24/mysq..._rand.html
[eluser]Fantass[/eluser]
SOLVED: Code: $this->db->order_by('id', 'random'); Thanks all.
[eluser]CroNiX[/eluser]
That still uses RAND(). You will be sorry if you run that on a moderately large table. Google "mysql rand()" for a million articles on why this is a huge performance killer. It has to retrieve all results, assign a number to each one, etc, which can literally take minutes depending on the size of the table. Running shuffle() on the result would probably be a lot more efficient.
[eluser]aquary[/eluser]
I'd do "count" all the randomable rows, then use PHP rand() to pick some number within the range, then use the number as an offset with limit 1... It was the fastest way I could think of. |
Welcome Guest, Not a member yet? Register Sign In |