Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 25th, 2008, 6:03 AM   #1
Logical1
Programmer
 
Logical1's Avatar
 
Join Date: Apr 2008
Posts: 47
Rep Power: 0 Logical1 is on a distinguished road
Question Order by RAND()

I am trying to pick up a random record (column ColName) form a MySQL table (tblExample).
All references that I have checked say that use ORDER BY RAND():

select ColName from tblExample ORDER by RAND()
Which I have been doing. But instead of 1 record I am getting the entire set of records. If the table has 20 records I am getting all the 20!

I have tried adding Limit 0,1 in the end but I keep on getting error messages as soon as LIMIT is entered into the select query.


Any idea as what is the problem?
Thanks in advance
L1
__________________
^c^
Logical1 is offline   Reply With Quote
Old Jun 25th, 2008, 1:10 PM   #2
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 162
Rep Power: 4 Arla is on a distinguished road
Re: Order by RAND()

Did you try select top(1) from tblexample order by rand()

That seems to be what I've found, although everything I've found has also said that using order by Rand() is horrific in performance terms.
Arla is offline   Reply With Quote
Old Jun 25th, 2008, 1:27 PM   #3
Logical1
Programmer
 
Logical1's Avatar
 
Join Date: Apr 2008
Posts: 47
Rep Power: 0 Logical1 is on a distinguished road
Re:

No I am simply trying to pick a random record and RAND() altough is s slow process should do the job.
May be I should change the questions:

I need to get a random record from a table of 2000 records. What is the best way to do this.
Table is structured like this:

ID Col1 Col2 Col3 Category
1 Joe Jane Jean Part time
2 Mike Ted Rose Full time
3 Ahmed Hans Mina Part time

I would like to show a random part time Id. Both Category and Id are factors in selection so I can not first generate a random number and open that number for Id.

Any suggestion is apreciated
L1
__________________
^c^
Logical1 is offline   Reply With Quote
Old Jun 25th, 2008, 1:52 PM   #4
Apophis
Newbie
 
Join Date: Apr 2008
Posts: 16
Rep Power: 0 Apophis is on a distinguished road
Re:

i think the query should be changed to: select ColName from tblExample where ID = [Random Number]
Apophis is offline   Reply With Quote
Old Jun 25th, 2008, 2:17 PM   #5
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Order by RAND()

Retrieve the entries you want into an array and then use the array_rand function provided by php.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Jun 25th, 2008, 5:00 PM   #6
Logical1
Programmer
 
Logical1's Avatar
 
Join Date: Apr 2008
Posts: 47
Rep Power: 0 Logical1 is on a distinguished road
How random is rand??

Thanks for suggestions guys.
Grimpirate
I did some tests including using a much larger table and trying to see how often Rand will repeat.
Results are appaling. 2 out of 10 times the very first record is repeated and one can see a patern of repeated numbers.
Is RAND like this or am I doing something wrong?

Apopis that won't work I need to use the category also.
__________________
^c^
Logical1 is offline   Reply With Quote
Old Jun 25th, 2008, 5:06 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Order by RAND()

Are you following the documentation?

See: array_rand()

Make sure you seed the random number generator with srand(). Also, specify how many random elements you need to pull from the array in the function's optional parameter (instead of making the naive mistake of pulling them one by one with array_rand($input)).

Follow this example code, and replace the $input assignment with the result of your database lookup.

<?php
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Link Order matthewvb PHP 1 Dec 20th, 2006 12:13 AM
CAD Based Order Processing? thriller Visual Basic .NET 3 Feb 8th, 2006 11:30 AM
Sorting order by scorpiosage PHP 3 Feb 15th, 2005 9:17 PM
rand() and rolling dice problem _MB_ C++ 4 Jan 22nd, 2005 12:25 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC