Re: selecting rows tagged with "a" but not "b" - Mailing list pgsql-sql

From Yeb Havinga
Subject Re: selecting rows tagged with "a" but not "b"
Date
Msg-id 4B66EAB4.4020900@gmail.com
Whole thread Raw
In response to Re: selecting rows tagged with "a" but not "b"  (Andreas Kretschmer <akretschmer@spamfence.net>)
List pgsql-sql
Andreas Kretschmer wrote:
> 8q5tmkyqry@sneakemail.com <8q5tmkyqry@sneakemail.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




pgsql-sql by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: selecting rows tagged with "a" but not "b"
Next
From: Adam Sherman
Date:
Subject: Crosstab Confusion