[RESOLVED] PHP/MySQL Random Record selection[Help Needed]
pls how do you select random datas from a MySQL database(really random).
say i have numerous records and i want to select 4(or more) random records each time the page is refreshed or re-visited.
i have read some tutorial/s but it doesn't help my situation and i have also read about the MySQL RAND() function(but i couldn't use it).
thank you.
Re: PHP/MySQL Random Record selection[Help Needed]
Why can you not use the mysql RAND() function? Do you have some kind of moral objection to it?
Re: PHP/MySQL Random Record selection[Help Needed]
You use it in the order by statement:
Code:
SELECT * FROM table ORDER BY RAND();
Re: PHP/MySQL Random Record selection[Help Needed]
oh!, from what i read, i thought you can only use RAND() for just one record as in RAND(0, 1).
Re: PHP/MySQL Random Record selection[Help Needed]
Well you can randomize the order of one record if you like. It will be a little it of pointless process :D
Re: PHP/MySQL Random Record selection[Help Needed]
Do not use RAND in an ORDER BY clause. This is an O(n) operation which is very slow for large data sets.
Instead, to pick one record at random, just generate a random number using PHP and use that in a LIMIT clause. You can use a SELECT COUNT() query to get the number of records in the table.
PHP Code:
$count = $db->query_read_scalar('SELECT COUNT(m.keyid) FROM mytable m');
$random_record = $db->query_read('SELECT m.* FROM mytable m LIMIT '.rand(0, $count).', 1');
This is O(n) too, but much faster.
Re: PHP/MySQL Random Record selection[Help Needed]
Re: PHP/MySQL Random Record selection[Help Needed]
This is only usually an issue when you have a very large dataset. I should have mentioned it because I ran into problems with it before :(
I used something like this which is still within SQL query itself: http://forums.mysql.com/read.php?24,...316#msg-192316
Re: PHP/MySQL Random Record selection[Help Needed]
Yes, that's the way used in the "better solution" above.
Re: PHP/MySQL Random Record selection[Help Needed]
The one I found is better because it is on the MySQL forum :p
Re: PHP/MySQL Random Record selection[Help Needed]
Quote:
Originally Posted by
visualAd
Why can you not use the mysql RAND() function? Do you have some kind of moral objection to it?
Correct me if I am wrong, but this doesn't actually return a random row. All it does is randomly select a column by which to order your results. This may give the appearance of random row, especially if your table has loads of columns. But what if it only has one column? You'll get the same order every time. :afrog:
Re: PHP/MySQL Random Record selection[Help Needed]
Quote:
Originally Posted by
loquela
Correct me if I am wrong, but this doesn't actually return a random row. All it does is randomly select a column by which to order your results. This may give the appearance of random row, especially if your table has loads of columns. But what if it only has one column? You'll get the same order every time. :afrog:
I am not quite sure what you are getting at.