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?
You can use this (if using MySQL):
ORM::factory('some_model')->order_by(DB::expr('RAND()'))->find();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With