-2

ex:subject type means g.k,english,maths etc... like that. my question is for preparing questionpapers by randomly selecting the questions of different questions

Can you please guide regarding this query....?

cathulhu
  • 641
  • 1
  • 9
  • 20
  • 1
    It's very unclear to me. Can't you just retrieve all questions and perform randomization on that? – Jeroen Vannevel Jun 27 '13 at 11:44
  • What is your db schema? – cathulhu Jun 27 '13 at 11:45
  • It means that we will have a table which contains questions of all categories(eng,maths,science,social ...etc).we need to select questions from that table randomly ,which we will give input to that like , the result should contain questions that, from maths-3,science-6,g.k-5 – user2527874 Jun 27 '13 at 11:50
  • This is a very poorly articulated question. Reading through the comments, I think what you're trying to ask is "How can I randomly select x number of questions from each category?" – Strawberry Jun 27 '13 at 12:12
  • Search up one of the million greatest-n-per-group questions here on SO and combine it with ORDER BY RAND(); – fancyPants Jun 27 '13 at 12:31

3 Answers3

1

Maybe not the best solution but should work: use a SELECT Query selecting the top 10 or 20 rows from a query ordered by RAND

eg:

SELECT * FROM questions_table WHERE subject='maths' ORDER BY RAND() LIMIT 0, 10

Sathish D
  • 4,854
  • 31
  • 44
Matt.C
  • 1,327
  • 7
  • 20
1
$sub1=$_POST['sub1'];
$no1=$_POST['no1'];

$sub2=$_POST['sub2'];
$no2=$_POST['no2'];

(assuming you are getting these values from a form)

mysql_query=('SELECT question FROM question_bank WHERE subject="'.$subj1.'" ORDER BY RAND() LIMIT $no1;

UNION

SELECT question FROM question_bank WHERE subject="'.$subj2.'" ORDER BY RAND() LIMIT $no2;')

.... so on... for all the subjects

fancyPants
  • 50,732
  • 33
  • 89
  • 96
kevinm
  • 475
  • 2
  • 7
  • thank you sir.another question after selecting 50 questions using above query ,we need to divide 50 questions into 5 sets means that each set must contain 10 ques(assume) – user2527874 Jun 27 '13 at 12:41
0

If you want questions from all subjects you can use :

SELECT question FROM question_bank ORDER BY RAND() LIMIT 50;

here 50 is the maximum number of rows in the table upto which you want to search and select the questions.

Now, If you want a subject-wise selection, you can use

SELECT question FROM question_bank WHERE subject='GK' ORDER BY RAND() LIMIT 50;
fancyPants
  • 50,732
  • 33
  • 89
  • 96
kevinm
  • 475
  • 2
  • 7