Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Is there a good way of retrieving entries randomly from a database (MySQL perhaps)?

by soon_j (Scribe)
on Jan 04, 2004 at 08:41 UTC ( [id://318633]=note: print w/replies, xml ) Need Help??


in reply to Re: Is there a good way of retrieving entries randomly from a database (MySQL perhaps)?
in thread Is there a good way of retrieving entries randomly from a database (MySQL perhaps)?

Thanks for the suggestions! But, here's the complete scenario. The questions table have columns: ques_id (PRIMARY KEY AUTO_INCREMENT), subject (VARCHAR), category_id (INT UNSIGNED), option_a (VARCHAR), ... option_d (VARCHAR). Another table (categories), have the following columns: category_id (PRIMARY KEY AUTO_INCREMENT), subject (VARCHAR), category (VARCHAR).

First, I have to retrieve all DISTINCT subjects from the categories table. (Example, I retrieve the following distinct subjects: Biology, Astronomy,... Mathematics). Since the user configures which subjects he wants to include on his exam, then I really have to do that. So this what I did (SELECT DISTINCT subject FROM categories).

Next, I have to retrieve also the categories under the selected subject. (Example, I have to retrieve the ff: categories under the subject Mathematics: Algebra, Geometry,... Calculus). So I have to do this (SELECT ques_id FROM questions WHERE subject='Subject1' and Category='Category1' ORDER BY RAND()). This routine would be repeated for each subject.

After each query described above, but before starting a new query for the next subject, I pushed the random results to an array. On the next subject, the results would be added or pushed to the array previously described.

This would result into an array having random ques_id's. However, though random the results are from each query, they are clustered according to subjects. Like, for instance, the first ten elements may be about Biology, the next 10 might be of Astronomy, and so forth. This is the reason why I have to randomize again the elements in the resulting array to make sure they are truly random.

Since the number of subjects could vary... like if a subject Religion was added to the existing subjects (Biology, Astronomy,...), I did not issue a single query like (SELECT ques_id FROM questions WHERE (subject='Subject1' and category='Category1') OR (subject='Subject2' and category='Category2')). If only I could issue a single effective query (which I haven't had a good idea yet), your suggestion of (SELECT ques_id FROM ... ORDER BY RAND() LIMIT 1) could be extremely useful. But with this type of query, would there be a chance that the same question would be repeated? Looks like... or is MySQL smart enough to know and avoid repeated results from RAND() LIMIT 1?

Your next suggestion of creating a table and dumping the randomized results into it is a good alternative that I have to try.

To wrap up, is there a good way to issue a single query so that I don't have to randomize the elements of my resulting array? For example, if I am the user, and I selected the subjects: Mathematics (with the following categories: Algebra, Geometry) and Astronomy (with the categories: Solary System, Astronomers), what would be a good way of issue a query? Without doing my lousy way of doing a query for each subject?

Thanks! JAY
  • Comment on Re: Re: Is there a good way of retrieving entries randomly from a database (MySQL perhaps)?

Replies are listed 'Best First'.
Re: Re: Re: Is there a good way of retrieving entries randomly from a database (MySQL perhaps)?
by sgifford (Prior) on Jan 04, 2004 at 09:53 UTC

    MySQL may repeat results if you do repeated queries with ORDER BY rand LIMIT 1. But it won't if you ask for all 10 questions at once---ORDER BY rand LIMIT 10.

    You may have to issue one query per subject, but so what? Unless there are dozens of subjects, the speed difference won't be noticeable.

Re: Re: Re: Is there a good way of retrieving entries randomly from a database (MySQL perhaps)?
by Anonymous Monk on Jan 04, 2004 at 09:00 UTC
    SELECT ques_id FROM questions q, categories c WHERE q.category_id = c.category_id AND category = 'Mathematics' AND subject in ('Calculus', 'Geometry', 'Algebra') ORDER BY RAND() LIMIT 1
      Just re-writing it, with a bit of correction (not a syntax type):

      SELECT ques_id
      FROM questions q, categories c
      WHERE q.category_id = c.category_id
      AND subject = 'Mathematics'
      AND category in ('Calculus', 'Geometry', 'Algebra')
      ORDER BY RAND()
      LIMIT 1

      The above query is for the subject 'Mathematics' only. How about for the other subjects? I am try to avoid repeating the above query for the next subject like Astronomy perhaps. I am try to consolidate all these tiny queries for each subject into a single and systematic query.

      Thanks! Jay

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://318633]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-25 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found