pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Select rows when all all ids of its children records matches
9+ messages / 5 participants
[nested] [flat]

* Select rows when all all ids of its children records matches
@ 2018-09-12 15:23 Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Arup Rakshit @ 2018-09-12 15:23 UTC (permalink / raw)
  To: pgsql-general

I have a table posts(id, name), posts_tags(post_id, tag_id) and tags (id, name) ... I want to get all posts which has tag id 1, 2 and 3 for example. How should I do this? I tried ALL, but it didn’t work.

Those tag ids comes from UI by users, so I am looking for generic approach.


Thanks,

Arup Rakshit
ar@zeit.io

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
@ 2018-09-12 15:28 ` Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  0 siblings, 1 reply; 9+ messages in thread

From: Ron @ 2018-09-12 15:28 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Maybe this:
select p.id, p.name
from posts p,
      posts_tags pt,
      tags t
where t.id in (1, 2, 3)
   and t.id = pt.tag_id
   and pt.post_id = p.id;


On 09/12/2018 10:23 AM, Arup Rakshit wrote:
> I have a table posts(id, name), posts_tags(post_id, tag_id) and tags (id, 
> name) ... I want to get all posts which has tag id 1, 2 and 3 for example. 
> How should I do this? I tried ALL, but it didn’t work.
>
> Those tag ids comes from UI by users, so I am looking for generic approach.
>
>
> Thanks,
>
> Arup Rakshit
> ar@zeit.io <mailto:ar@zeit.io>
>
>
>

-- 
Angular momentum makes the world go 'round.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
@ 2018-09-12 15:30   ` Arup Rakshit <ar@zeit.io>
  2018-09-12 15:43     ` Re: Select rows when all all ids of its children records matches David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Arup Rakshit @ 2018-09-12 15:30 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

IN is OR, I want the AND logic. Select posts which has tag 1, 2 and 3 ( tag ids )


Thanks,

Arup Rakshit
ar@zeit.io



> On 12-Sep-2018, at 8:58 PM, Ron <ronljohnsonjr@gmail.com> wrote:
> 
> Maybe this:
> select p.id, p.name
> from posts p,
>      posts_tags pt,
>      tags t
> where t.id in (1, 2, 3)
>   and t.id = pt.tag_id
>   and pt.post_id = p.id;
> 
> 
> On 09/12/2018 10:23 AM, Arup Rakshit wrote:
>> I have a table posts(id, name), posts_tags(post_id, tag_id) and tags (id, name) ... I want to get all posts which has tag id 1, 2 and 3 for example. How should I do this? I tried ALL, but it didn’t work.
>> 
>> Those tag ids comes from UI by users, so I am looking for generic approach.
>> 
>> 
>> Thanks,
>> 
>> Arup Rakshit
>> ar@zeit.io <mailto:ar@zeit.io>
>> 
>> 
>> 
> 
> -- 
> Angular momentum makes the world go 'round.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
@ 2018-09-12 15:43     ` David G. Johnston <david.g.johnston@gmail.com>
  2018-09-12 15:44       ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  0 siblings, 1 reply; 9+ messages in thread

From: David G. Johnston @ 2018-09-12 15:43 UTC (permalink / raw)
  To: Arup Rakshit <ar@zeit.io>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Wednesday, September 12, 2018, Arup Rakshit <ar@zeit.io> wrote:

> IN is OR, I want the AND logic. Select posts which has tag 1, 2 and 3 (
> tag ids )
>

Build arrays and then use the “contains” operator.

David J.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:43     ` Re: Select rows when all all ids of its children records matches David G. Johnston <david.g.johnston@gmail.com>
@ 2018-09-12 15:44       ` Arup Rakshit <ar@zeit.io>
  2018-09-12 15:57         ` Re: Select rows when all all ids of its children records matches Alban Hertroys <haramrae@gmail.com>
  2018-09-12 16:08         ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  0 siblings, 2 replies; 9+ messages in thread

From: Arup Rakshit @ 2018-09-12 15:44 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Can you show me the SQL construction? Do I need to use `WITH`?


Thanks,

Arup Rakshit
ar@zeit.io



> On 12-Sep-2018, at 9:13 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
> 
> On Wednesday, September 12, 2018, Arup Rakshit <ar@zeit.io <mailto:ar@zeit.io>> wrote:
> IN is OR, I want the AND logic. Select posts which has tag 1, 2 and 3 ( tag ids )
> 
> Build arrays and then use the “contains” operator.
> 
> David J. 

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:43     ` Re: Select rows when all all ids of its children records matches David G. Johnston <david.g.johnston@gmail.com>
  2018-09-12 15:44       ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
@ 2018-09-12 15:57         ` Alban Hertroys <haramrae@gmail.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Alban Hertroys @ 2018-09-12 15:57 UTC (permalink / raw)
  To: Arup Rakshit <ar@zeit.io>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>


> On 12 Sep 2018, at 17:44, Arup Rakshit <ar@zeit.io> wrote:
> 
> Can you show me the SQL construction? Do I need to use `WITH`?


An option is to create a bit-wise OR and SUM the grouped results. If the result of these 3 bits is 7, than the post matches all three tags.

select p.id, p.name
  from post p
  join post_tag pt on (pt.post = p.id)
  join tag t on (t.id = pt.tag)
 where t.id in (1, 2, 3)
 group by
  case t.id
   when 1 then 1
   when 2 then 2
   when 3 then 4
   else 0
  end
 having sum(case t.id
   when 1 then 1
   when 2 then 2
   when 3 then 4
   else 0
  end) = 7;

I used ints here for the bitwise OR, a bitstring would probably be neater.

Another approach is to aggregate the set of matching tags into an array using array_agg(). I think that's what David means. You could then check the length of the array to see if you have all 3 (or 4 or 5 or 9000).

>> On 12-Sep-2018, at 9:13 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
>> 
>> On Wednesday, September 12, 2018, Arup Rakshit <ar@zeit.io> wrote:
>> IN is OR, I want the AND logic. Select posts which has tag 1, 2 and 3 ( tag ids )
>> 
>> Build arrays and then use the “contains” operator.
>> 
>> David J. 
> 

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:43     ` Re: Select rows when all all ids of its children records matches David G. Johnston <david.g.johnston@gmail.com>
  2018-09-12 15:44       ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
@ 2018-09-12 16:08         ` Arup Rakshit <ar@zeit.io>
  2018-09-12 16:17           ` Re: Select rows when all all ids of its children records matches Rob Sargent <robjsargent@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Arup Rakshit @ 2018-09-12 16:08 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

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 FROM posts_tags_cte JOIN posts ON posts.id = posts_tags_cte.post_id
WHERE posts_tags_cte.tags @> array[1, 2]::int8[]

But it gives me all the posts.


Thanks,

Arup Rakshit
ar@zeit.io



> On 12-Sep-2018, at 9:14 PM, Arup Rakshit <ar@zeit.io> wrote:
> 
> Can you show me the SQL construction? Do I need to use `WITH`?
> 
> 
> Thanks,
> 
> Arup Rakshit
> ar@zeit.io <mailto:ar@zeit.io>
> 
> 
> 
>> On 12-Sep-2018, at 9:13 PM, David G. Johnston <david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>> wrote:
>> 
>> On Wednesday, September 12, 2018, Arup Rakshit <ar@zeit.io <mailto:ar@zeit.io>> wrote:
>> IN is OR, I want the AND logic. Select posts which has tag 1, 2 and 3 ( tag ids )
>> 
>> Build arrays and then use the “contains” operator.
>> 
>> David J. 
> 

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:43     ` Re: Select rows when all all ids of its children records matches David G. Johnston <david.g.johnston@gmail.com>
  2018-09-12 15:44       ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 16:08         ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
@ 2018-09-12 16:17           ` Rob Sargent <robjsargent@gmail.com>
  2018-09-12 16:19             ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  0 siblings, 1 reply; 9+ messages in thread

From: Rob Sargent @ 2018-09-12 16:17 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org



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]?

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Select rows when all all ids of its children records matches
  2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:28 ` Re: Select rows when all all ids of its children records matches Ron <ronljohnsonjr@gmail.com>
  2018-09-12 15:30   ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 15:43     ` Re: Select rows when all all ids of its children records matches David G. Johnston <david.g.johnston@gmail.com>
  2018-09-12 15:44       ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 16:08         ` Re: Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
  2018-09-12 16:17           ` Re: Select rows when all all ids of its children records matches Rob Sargent <robjsargent@gmail.com>
@ 2018-09-12 16:19             ` Arup Rakshit <ar@zeit.io>
  0 siblings, 0 replies; 9+ messages in thread

From: Arup Rakshit @ 2018-09-12 16:19 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Hi Rob,

I figured it out. thanks. It is giving correct data.

> Aren't you looking for cte.tags = array[1,2]?

posts_tags_cte has tags column, so I am using it.

Thanks,

Arup Rakshit
ar@zeit.io



> On 12-Sep-2018, at 9:47 PM, Rob Sargent <robjsargent@gmail.com> wrote:
> 
> 
> 
> 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]?
> 

^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2018-09-12 16:19 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-09-12 15:23 Select rows when all all ids of its children records matches Arup Rakshit <ar@zeit.io>
2018-09-12 15:28 ` Ron <ronljohnsonjr@gmail.com>
2018-09-12 15:30   ` Arup Rakshit <ar@zeit.io>
2018-09-12 15:43     ` David G. Johnston <david.g.johnston@gmail.com>
2018-09-12 15:44       ` Arup Rakshit <ar@zeit.io>
2018-09-12 15:57         ` Alban Hertroys <haramrae@gmail.com>
2018-09-12 16:08         ` Arup Rakshit <ar@zeit.io>
2018-09-12 16:17           ` Rob Sargent <robjsargent@gmail.com>
2018-09-12 16:19             ` Arup Rakshit <ar@zeit.io>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox