Skip site navigation (1) Skip section navigation (2)

Re: Best way to "and" from a one-to-many joined table?

From: Bryce Nesbitt <bryce2(at)obviously(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: milan(dot)opa(at)gmail(dot)com
Subject: Re: Best way to "and" from a one-to-many joined table?
Date: 2008-12-10 18:54:29
Message-ID: 49401065.80700@obviously.com (view raw or flat)
Thread:
Lists: pgsql-sql

Milan Oparnica wrote:
> This is how I do it, and it runs fast:
> select p.*
> from test_people p inner join test_attributes a on p.people_id =
> a.people_id
> where a."attribute" = @firstAttr or a."attribute" = @secondAttr
But that does an "or" search, not "and", returning Satan in addition to
Obama:

select * from test_people p inner join test_attributes a on p.people_id
= a.people_id
lyell5-> where a."attribute" = 'Dark Hair' or a."attribute" = 'USA
President';
+-----------+-------------+-----------+---------------+
| people_id | person_name | people_id |   attribute   |
+-----------+-------------+-----------+---------------+
|         8 | Obamba      |         8 | USA President |
|         8 | Obamba      |         8 | Dark Hair     |
|         8 | Obamba      |         8 | Dark Hair     |
|        10 | Satan       |        10 | Dark Hair     |
+-----------+-------------+-----------+---------------+

How can I get an AND search (people with Dark Hair AND who are President)?


In response to

Responses

pgsql-sql by date

Next:From: Oliveiros CristinaDate: 2008-12-10 19:21:02
Subject: Re: Best way to "and" from a one-to-many joined table?
Previous:From: Richard HuxtonDate: 2008-12-10 17:45:06
Subject: Re: Is there a bug in PostgreSQL ?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group