If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Read data from a MySQL DB un-randomly

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-08, 08:59
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Read data from a MySQL DB un-randomly

Hi,

I am a new member here, looking for a solution to a problem I have. I
am building a PHP web app, and am looking for a means to get data
from
a mysql DB in a not random manner (sequentially or however else).
Think of it as a Quote of the Day feature, with all quotes in the DB
having equal chances of being shown. It could be a random pick, but
in
a way that guarantees fairness. I have thought of just getting the
data sequentially but I am not sure its the best way, and how best to
implement this.


Please Assist.


Grav
Reply With Quote
  #2 (permalink)  
Old 10-20-08, 09:25
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
I'd have a look at the rand function
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 10-20-08, 11:10
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Quote:
Originally Posted by healdem
I'd have a look at the rand function
Healdem,

I want all the entries in the DB to have an equal chance of being shown, whether sequentially or any other way. So how would rand work?
Reply With Quote
  #4 (permalink)  
Old 10-20-08, 11:38
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Does this air-code work?
Code:
SELECT *
FROM   quotations
ORDER
    BY Rand()
LIMIT 1;
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 10-20-08, 12:58
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Quote:
Originally Posted by georgev
Does this air-code work?
Code:
SELECT *
FROM   quotations
ORDER
    BY Rand()
LIMIT 1;
Not really, its not fair (some entries get selected multiple times before others are) which is not really what I want..
Reply With Quote
  #6 (permalink)  
Old 10-20-08, 13:18
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
so where are your multiple entries coming from. the rand function can be used to select random records. Ive used this technique to display random images.. Ive yet to see duplicate images, admittedly I can't guarantee thee are not duplicates but Ive yet to see them

is the reason you are seeing duplicate because of your table design is it your data, is it becuase the query needs refining

have you read the references and examples that can be found on the web

what is your table definition, where are you seeing multiple rows.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 10-21-08, 05:02
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Quote:
Originally Posted by healdem
so where are your multiple entries coming from. the rand function can be used to select random records. Ive used this technique to display random images.. Ive yet to see duplicate images, admittedly I can't guarantee thee are not duplicates but Ive yet to see them

is the reason you are seeing duplicate because of your table design is it your data, is it becuase the query needs refining

have you read the references and examples that can be found on the web

what is your table definition, where are you seeing multiple rows.
You misunderstood me healdem, I am not getting multiple rows but if I run the query 3 times I may get the same result 3 times (since its random), which is not what I want. I want an equal distribution.
Reply With Quote
  #8 (permalink)  
Old 10-21-08, 06:17
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
Ive not seen that either
as said before Ive used rand to select random images and I have never seen the same images repeating, occasionally you get a duplicate but thats part of the random nature. rand doesn't guarantee you will NEVER get duplicates... it doenst' guarantee that all items will be selected.. however the probability is that you will get all items selected over time

are you using the function as documented on the MySQL website and elsewhere
what is the SQL you are using
are you setting a seed value for the rand function
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #9 (permalink)  
Old 10-21-08, 06:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by healdem
occasionally you get a duplicate but thats part of the random nature. rand doesn't guarantee you will NEVER get duplicates...
but that's what he wants -- he wants all of them, randomly, but never the same one twice
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-21-08, 06:57
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
in which case he needs to maintain a list of items that have been selected (or not selected as the case may be) and then randomly select from that list.


and thats before we start getting involved on how random a computer random number is
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #11 (permalink)  
Old 10-21-08, 07:23
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
What about this for a solution

Pseudo code
Code:
Parameters: @current_id

Variable: @new_id

SELECT @new_id = Min(id)
FROM   quotations
WHERE  id > @current_id

IF @new_id IS NULL THEN
  SELECT @new_id  = Min(id)
  FROM   quotations
END IF

RETURN @new_id
Ultimately it's just cycling through the records based on an id (easily could be a different field)...
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On