List:General Discussion« Previous MessageNext Message »
From:Andre Polykanine Date:February 13 2011 11:39pm
Subject:Simplify 4 queries into 1
View as plain text  
Hi all,
Hope this question is appropriate here :-).
I've got 4 queries:

$q1=mysql_query("SELECT     *    FROM    `CandidateQuestions`    WHERE
`Category`='1' ORDER BY RAND() LIMIT 1");
$q2=mysql_query("SELECT     *    FROM    `CandidateQuestions`    WHERE
`Category`='2' ORDER BY RAND() LIMIT 1");
$q3=mysql_query("SELECT     *    FROM    `CandidateQuestions`    WHERE
`Category`='3' ORDER BY RAND() LIMIT 1");
$q4=mysql_query("SELECT     *    FROM    `CandidateQuestions`    WHERE
`Category`='4' ORDER BY RAND() LIMIT 1");

What  I  need  is  to  select exactly 4 questions so that each of them
would be randomly selected from each category.
Note:  I  don't need 4 *any* questions, I need 1 question from the 1st
category, 1 question from the 2nd category etc.
Is there a way to make one query for this?
If I do
"SELECT  *  FROM  `CandidateQuestions`  WHERE  `Category` IN (1,2,3,4)
GROUP BY `Category` LIMIT 4",
I think i would get 4 random questions, wouldn't I?
Thanks in advance!

  

-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: http://twitter.com/m_elensule
Facebook: http://facebook.com/menelion

Thread
Simplify 4 queries into 1Andre Polykanine14 Feb
  • Re: Simplify 4 queries into 1mos14 Feb