Re: Simplifying MySql queries
| From: | Thijs Lensselink | Date: | Sat, 12 Feb 2011 20:47:09 +0000 |
| Subject: | Re: Simplifying MySql queries | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-311259@lists.php.net to get a copy of this message | ||
On 02/12/2011 09:40 PM, Andre Polykanine wrote:
> Hi all,
> I'm using in my PHP script the following four MySql 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");
>
> and here goes the question: is there a way to make these four in one
> so strictly one random question is selected from all of the four
> categories?
> Thanks!
>
You really should ask this on a SQL mailing list. it doesn't have much
to do with PHP.
That said. You could use the IN() statement like this:
SELECT * FROM CandidateQuestions WHERE Category IN (1, 2, 3) ORDER
BY RAND() LIMIT 1;