From: | David Fetter <david(at)fetter(dot)org> |
---|---|
To: | Sam Mason <sam(at)samason(dot)me(dot)uk> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Find users that have ALL categories |
Date: | 2010-07-01 11:43:19 |
Message-ID: | 20100701114319.GB18804@fetter.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Thu, Jul 01, 2010 at 12:37:55PM +0100, Sam Mason wrote:
> On Thu, Jul 01, 2010 at 04:26:38AM -0700, David Fetter wrote:
> > On Wed, Jun 30, 2010 at 12:11:35AM -0700, Nick wrote:
> > > Is this the most efficient way to write this query? Id like to
> > > get a list of users that have the categories 1, 2, and 3?
> > >
> > > SELECT user_id FROM user_categories WHERE category_id IN (1,2,3)
> > > GROUP BY user_id HAVING COUNT(*) = 3
> >
> > The above method depends on (user_id, category_id) being unique,
> > and excludes users with, say, categories 1, 2, 3 and 4. Are you
> > sure that that latter is what you want?
>
> AFAICT, the above code will include a user with categories 1 to 4.
> Why do you think otherwise?
>
> If the (user_id,category_id) combination isn't unique, it's easy to
> change the HAVING clause into HAVING COUNT(DISTINCT category_id) =
> 3.
Oops. You're right, of course. That's what I get for posting before
waking up. ;)
Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
From | Date | Subject | |
---|---|---|---|
Next Message | Andrew Geery | 2010-07-01 11:53:39 | extracting total amount of time from an interval |
Previous Message | Sam Mason | 2010-07-01 11:37:55 | Re: Find users that have ALL categories |