Re: mysql query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 13 Aug 2004 19:39:59 +0200, Torsten Roehr <roehr@xxxxxxxxxxx> wrote:
> "Justin Patrin" <papercrane@xxxxxxxxx> wrote in message
> news:432beae04081310237fa77048@xxxxxxxxxxxxxxxxx
> > On Fri, 13 Aug 2004 15:20:36 +0200, Quentin Cotillard
> > <quentin@xxxxxxxxxxxxxxxx> wrote:
> > > Consider a table similar to the one below.
> > > What I want to do is to get ONE random record from categories(cat) A
> > > and 5 random record from category B
> > >
> > > | ID | computer | name | cat |...
> > >    1    dell       834    A
> > >    2    ibm        526    A
> > >    3    apple      134    B
> > >    4    sony       333    A
> > >    5    dell       834    B
> > >    6    ibm        556    A
> > >    7    apple      534    B
> > >    8    sony       233    A
> > >    9    dell       874    A
> > > ....
> > >
> > > How could I construct my query to the mysql?
> > >
> >
> > This is an SQL question, not a PHP question.
> >
> > order by rand limit 5
> 
> Hi Justin,
> 
> this won't work because he needs to make sure to get 1 from category A *AND*
> 5 from category B with one query. I had a similar problem some weeks ago and
> even though a lot of people from the list were trying to help we didn't find
> a solution. In the end I had to do seperate queries.
> 

I actually assumed that. This *can* be done in a query, but it
requires sub-queriesunions. Something like this:

SELECT * FROM computer WHERE cat = 'A' ORDER BY rand() LIMIT 1
UNION
SELECT * FROM computer WHERE cat = 'B' ORDER BY rand() LIMIT 5

-- 
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux