|
-
Mar 7th, 2008, 04:06 PM
#1
Thread Starter
Fanatic Member
[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.
-
Mar 7th, 2008, 07:10 PM
#2
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?
-
Mar 7th, 2008, 07:12 PM
#3
Re: PHP/MySQL Random Record selection[Help Needed]
You use it in the order by statement:
Code:
SELECT * FROM table ORDER BY RAND();
-
Mar 8th, 2008, 10:21 AM
#4
Thread Starter
Fanatic Member
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).
-
Mar 8th, 2008, 11:40 AM
#5
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
-
Mar 8th, 2008, 02:03 PM
#6
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.
Last edited by penagate; Mar 8th, 2008 at 02:09 PM.
-
Mar 8th, 2008, 03:21 PM
#7
Re: PHP/MySQL Random Record selection[Help Needed]
-
Mar 8th, 2008, 04:01 PM
#8
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
-
Mar 8th, 2008, 04:04 PM
#9
Re: PHP/MySQL Random Record selection[Help Needed]
Yes, that's the way used in the "better solution" above.
-
Mar 8th, 2008, 04:08 PM
#10
Re: PHP/MySQL Random Record selection[Help Needed]
The one I found is better because it is on the MySQL forum
-
Jun 30th, 2009, 09:39 AM
#11
New Member
Re: PHP/MySQL Random Record selection[Help Needed]
 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.
-
Jul 1st, 2009, 04:30 AM
#12
Re: PHP/MySQL Random Record selection[Help Needed]
 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. 
I am not quite sure what you are getting at.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|