Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2002, 05:33 AM   PM User | #1
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
Random Row

Hey guys,

I just have a quick question about grabbing a random row from a mysql table.. how do you do it?
I have tried "ORDER BY rand()" but it keeps picking the same row for some reason (maybe that reason can be explained).
Any suggestions would be greatly appreciated.
Thanks a lot,

Matthew
nurseryboy is offline   Reply With Quote
Old 11-06-2002, 11:35 AM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Try changing ORDER BY to LIMIT.

Just a shot in the dark
bcarl314 is offline   Reply With Quote
Old 11-06-2002, 02:41 PM   PM User | #3
Galdo
New Coder

 
Join Date: Aug 2002
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Galdo is an unknown quantity at this point
LIMIT is used to specify the number of rows returned, so isn't really appropriate here as far as i know. I would suggest something like:

PHP Code:
srand((double)microtime()*1000000);
$randnumber rand(1,NUMBER); 
Then select the row using the random number generated:

SELECT * from TABLE WHERE RowID = '$randnumber'

RowID should be some sort of auto incremented field using to identify rows. The 'NUMBER' above should be changed to the total number of rows in your table to avoid creating a random number which is more than your number of rows.
Galdo is offline   Reply With Quote
Old 11-06-2002, 02:56 PM   PM User | #4
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
Ok,
If I use php to generate a random number, and then grab that number from an autoincremented column on the table, wont that mess up if the number isnt there? It is grabbing information that will be added and deleted as time goes on. So autoincremented numbers will be lost here and there. Ex: The number of rows may be 30, but the highest autoincrement may be 50, because 20 rows have been deleted.
I hope that makes sense. If so, how can I fix that part of it?
Thanks a lot,

Matthew
nurseryboy is offline   Reply With Quote
Old 11-06-2002, 08:12 PM   PM User | #5
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
Do you run an old version of MySQL? Because

SELECT * FROM table ORDER BY RAND() LIMIT 1;

works very good for me... and should so since MySQL 3.23.2
mordred is offline   Reply With Quote
Old 11-07-2002, 02:14 AM   PM User | #6
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
I am running version 3.23.52.
ORDER BY RAND() keeps grabbing the same row for some reason.
But, when I try to grab a random row from another table, it seems to work fine..
Weird.. lol.

Matthew

Last edited by nurseryboy; 11-07-2002 at 03:11 AM..
nurseryboy is offline   Reply With Quote
Old 11-07-2002, 04:36 PM   PM User | #7
Jeewhizz
Regular Coder


 
Join Date: May 2002
Location: London, England
Posts: 369
Thanks: 0
Thanked 0 Times in 0 Posts
Jeewhizz is an unknown quantity at this point
how many rows do you have in the table? if its only one row.. what do you think will happen
__________________
Jeewhizz - MySQL Moderator
http://www.sitehq.co.uk
PHP and MySQL Hosting
Jeewhizz is offline   Reply With Quote
Old 11-07-2002, 05:52 PM   PM User | #8
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
Haha, yeah, I thought of that.
The fist thing I did was make sure I had at least two rows. That would be kinda dumb to be expecting something that wasnt there.. lol.
Good suggestion though. Keeping me on my feet.

Matthew
nurseryboy is offline   Reply With Quote
Old 11-08-2002, 12:59 AM   PM User | #9
Jeewhizz
Regular Coder


 
Join Date: May 2002
Location: London, England
Posts: 369
Thanks: 0
Thanked 0 Times in 0 Posts
Jeewhizz is an unknown quantity at this point
even if yo have two rows it could still bring up one row continually!
__________________
Jeewhizz - MySQL Moderator
http://www.sitehq.co.uk
PHP and MySQL Hosting
Jeewhizz is offline   Reply With Quote
Old 11-08-2002, 01:16 AM   PM User | #10
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
Why would it do that? I would think eventually it would pick the other? Maybe?

Matthew
nurseryboy is offline   Reply With Quote
Old 11-08-2002, 01:27 AM   PM User | #11
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Quote:
Originally posted by nurseryboy
Why would it do that? I would think eventually it would pick the other? Maybe?

Matthew
It would eventually but it would be much easier to have ten or more rows in there.

So it's probably working just fine.
oracleguy is offline   Reply With Quote
Old 11-08-2002, 02:20 AM   PM User | #12
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
I will add some more rows and try it out.
Thanks for the suggestion. Hopefully thats it.

Matthew
nurseryboy is offline   Reply With Quote
Old 11-08-2002, 03:00 AM   PM User | #13
nurseryboy
New Coder

 
Join Date: Oct 2002
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
nurseryboy is an unknown quantity at this point
Ok, I added a few more and it seems to work. It sticks to one row more than the others, but overall they are random.
Thanks a lot for the help, I think that was the problem.

Matthew
nurseryboy is offline   Reply With Quote
Old 11-08-2002, 02:53 PM   PM User | #14
Jeewhizz
Regular Coder


 
Join Date: May 2002
Location: London, England
Posts: 369
Thanks: 0
Thanked 0 Times in 0 Posts
Jeewhizz is an unknown quantity at this point
Glad we could help.... if you have two rows it could always go to the same row because its a 50/50 chance with 10 rows its a 10/90 chance... so more chance of getting another one!
__________________
Jeewhizz - MySQL Moderator
http://www.sitehq.co.uk
PHP and MySQL Hosting
Jeewhizz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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


All times are GMT +1. The time now is 10:39 PM.


Advertisement
Log in to turn off these ads.