Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql query for random id [duplicate]

Tags:

php

mysql

Possible Duplicate:
MySQL select 10 random rows from 600K rows fast

I need to pull from a table a random id. At this moment only a few records exists but with time they will grow. What methods of getting this id are in Php or MySql, and what are the trade offs, consequences between them. One last thing i need speed and performance.

like image 788
croppio.com Avatar asked Mar 19 '26 19:03

croppio.com


2 Answers

select * from YOUR_TABLE order by rand() limit 1
like image 53
rahularyansharma Avatar answered Mar 21 '26 07:03

rahularyansharma


You can achieve this direct in your SQL:

SELECT `idfield` FROM `table` ORDER BY RAND() LIMIT 0,1;

Also see here for some alternatives.

This will be a simple means of execution than building the randomisation in your PHP and then passing to mySQL, though the link above details the merits of the various approaches.

like image 24
SW4 Avatar answered Mar 21 '26 07:03

SW4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!