pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Arup Rakshit <ar@zeit.io>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: Ron <ronljohnsonjr@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Select rows when all all ids of its children records matches
Date: Wed, 12 Sep 2018 17:57:30 +0200
Message-ID: <0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com> (raw)
In-Reply-To: <EDC3D41A-E121-4245-BE30-AA017DDACA49@zeit.io>
References: <FF55BC81-BB56-45C3-BCA2-B7D0F8E81F4A@zeit.io>
	<fe916369-aa51-0e7b-3afa-eb89974dfcc4@gmail.com>
	<5E56CBFF-62E6-4C17-B6D8-BBC05BC10C8B@zeit.io>
	<CAKFQuwZN-Lk3ErCACMdQZYDiiCto0NkrgTKZNKshkgxN-iSd=A@mail.gmail.com>
	<EDC3D41A-E121-4245-BE30-AA017DDACA49@zeit.io>


> 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.





view thread (9+ messages)  latest in thread

Message-ID: <0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com>
Permalink:  ../0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com/
Also on:    postgresql.org/message-id/0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, ar@zeit.io, david.g.johnston@gmail.com, ronljohnsonjr@gmail.com
  Subject: Re: Select rows when all all ids of its children records matches
  In-Reply-To: <0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com>
  References: <FF55BC81-BB56-45C3-BCA2-B7D0F8E81F4A@zeit.io> <fe916369-aa51-0e7b-3afa-eb89974dfcc4@gmail.com> <5E56CBFF-62E6-4C17-B6D8-BBC05BC10C8B@zeit.io> <CAKFQuwZN-Lk3ErCACMdQZYDiiCto0NkrgTKZNKshkgxN-iSd=A@mail.gmail.com> <EDC3D41A-E121-4245-BE30-AA017DDACA49@zeit.io> <0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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