Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a random row from a database query in Kohana 3

I'm using the ORM module in Kohana 3 and instead of displaying the first row of a database result set, what query should I use in my code when I want to get a random row from a certain table?

like image 291
ed. Avatar asked Dec 24 '09 03:12

ed.


2 Answers

You can use this (if using MySQL):

ORM::factory('some_model')->order_by(DB::expr('RAND()'))->find();
like image 112
dusan Avatar answered Nov 11 '22 13:11

dusan


You can issue the query directly, if you are using MySQL:

SELECT * FROM table LIMIT 1 ORDER BY RAND();

Or with Kohona Query Builder:

$this->db->from('table')->select('*')->limit(1)->orderby(null, 'RAND()')->get();
like image 21
Alix Axel Avatar answered Nov 11 '22 11:11

Alix Axel