Re: Select rows when all all ids of its children records matches

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Select rows when all all ids of its children records matches
Date: 2018-09-12 16:17:54
Message-ID: b384bb1a-4281-4745-6a5f-9b474d9b4560@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 09/12/2018 10:08 AM, Arup Rakshit wrote:
> I tried :
>
> WITH posts_tags_cte AS (
>         SELECT post_id, array_agg(tag_id) as tags
>         FROM posts_tags
>         WHERE tag_id in (1, 2)
>         GROUP BY post_id
> )
> SELECT posts.id <http://posts.id> FROM posts_tags_cte JOIN posts ON
> posts.id <http://posts.id> = posts_tags_cte.post_id
> WHERE posts_tags_cte.tags @> array[1, 2]::int8[]
>
> But it gives me all the posts.
>
>
Aren't you looking for cte.tags = array[1,2]?

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Arup Rakshit 2018-09-12 16:19:18 Re: Select rows when all all ids of its children records matches
Previous Message Arup Rakshit 2018-09-12 16:08:04 Re: Select rows when all all ids of its children records matches