Re: selecting rows tagged with "a" but not "b"

From: Yeb Havinga <yebhavinga(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: selecting rows tagged with "a" but not "b"
Date: 2010-02-01 14:52:36
Message-ID: 4B66EAB4.4020900@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Andreas Kretschmer wrote:
> 8q5tmkyqry(at)sneakemail(dot)com <8q5tmkyqry(at)sneakemail(dot)com> wrote:
>
>
>> Hi,
>>
>> I have a two tables:
>>
>> article
>> articleID, name, content
>>
>> tags
>> articleID, tag
>>
>> I want to find all articles that are tagged with "a" but not "b"
>>
>> how do I do this?
>>
>
> select a.* from article left join tags t on a.articleID=t.articleID where b.tag = 'a';
>
select a.* from article left join tags t on a.articleID=t.articleID
where t.tag = 'a'
where not exists (select * from tags t2 where t2.articleID=a.articleID
and t2.tag = 'b');

Yeb

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Adam Sherman 2010-02-01 15:03:40 Crosstab Confusion
Previous Message Andreas Kretschmer 2010-02-01 14:14:53 Re: selecting rows tagged with "a" but not "b"