Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g07WO-0005UI-40 for pgsql-general@arkaria.postgresql.org; Wed, 12 Sep 2018 15:57:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g07WM-0007Xl-85 for pgsql-general@arkaria.postgresql.org; Wed, 12 Sep 2018 15:57:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g07WL-0007Xe-UR for pgsql-general@lists.postgresql.org; Wed, 12 Sep 2018 15:57:38 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g07WI-0006TX-Vo for pgsql-general@lists.postgresql.org; Wed, 12 Sep 2018 15:57:37 +0000 Received: by mail-ed1-x533.google.com with SMTP id f38-v6so2176286edd.8 for ; Wed, 12 Sep 2018 08:57:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=nnbGvZxm/jxm4EuDekAfPA/ovF/Lciha4DUz0VqflBs=; b=nnVAXPk7osJ5B6GZAwWw1fKad92YfdiUup+YIkIxCEVe6XHiLiapedGnqIH6eh31a3 IE63ZgRXMYuf2Ao2xemtlW0xJIsdn6DeUBc49+GbG60/F7j43Mvh/Xv/nxsCt0iqBfU0 6C5FSMgZW0mcAn5CEgBMkqhEPanXAZKAQxdrd6IlmQvYKKl1vQ1vmGHO06wrCzntu1fe F0cfyKFR4V9aWFJn4V5ICBOpquPQQizEN3pIbEYB59N84xkag5JwK8mFIsAEVIp+nvi/ J3MS2Q0tKdva0qbToaT3ae3U8AVGL+m3w0iMmzQ0u9lzifcVNECTdqfTqjygz0U3UjpC X9Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=nnbGvZxm/jxm4EuDekAfPA/ovF/Lciha4DUz0VqflBs=; b=BC1F5c5xKEQ8E96z5iq+0YqNePpcupzSAU6LyBQTnjkIZYs7+0jz8x4w8/gKIga3nH tMp3Cl7WjKJSjaCCGxt256q5Bq+fluCdwF1RCbNW8Cu/Lb3/xBFtlHp1utSaPdXwQdQm 63sMkfws/foJl2Yany24Aw5z4lX/8os8odIYEZFFmkLBFTUnHw9XoZtamwpCRVczVsbM HVPmSwjKGXnPS4FBFTG5pqPZTNR6V2IKoT6v9+qhlhaQXdxOEtW5KqsfupR5mUnkjVG7 5svu10H4DRLiBHPK+PVkFsXXxh/bEhrgiT+21I6iPwg6zSdvZ+6++66PWk6uJeMkaQCf sRJA== X-Gm-Message-State: APzg51Cj2XUXnrwBwglTZ7/0JUxwuk5lZIgqG7TZmsxomWKcoWh+3yVA lgMAEJ0fyyfQ34i8tjGzA4k= X-Google-Smtp-Source: ANB0Vdavq5s/hZ2FMxLVJpo3fJjyruYP/o++wR12FHisl6Vz7IuAi1vqYM0vfNbHtkMgl5iNkHbZ3A== X-Received: by 2002:a50:da01:: with SMTP id z1-v6mr3693518edj.62.1536767853177; Wed, 12 Sep 2018 08:57:33 -0700 (PDT) Received: from hollewijn.internal (84-245-13-9.dsl.cambrium.nl. [84.245.13.9]) by smtp.gmail.com with ESMTPSA id g19-v6sm677966edh.22.2018.09.12.08.57.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Sep 2018 08:57:32 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: Re: Select rows when all all ids of its children records matches From: Alban Hertroys In-Reply-To: Date: Wed, 12 Sep 2018 17:57:30 +0200 Cc: "David G. Johnston" , Ron , "pgsql-general@lists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <0F6A7C43-0ACA-4BA0-8D21-2C5EA24E714B@gmail.com> References: <5E56CBFF-62E6-4C17-B6D8-BBC05BC10C8B@zeit.io> To: Arup Rakshit X-Mailer: Apple Mail (2.3445.9.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 12 Sep 2018, at 17:44, Arup Rakshit wrote: >=20 > 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 =3D p.id) join tag t on (t.id =3D 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) =3D 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 = wrote: >>=20 >> On Wednesday, September 12, 2018, Arup Rakshit wrote: >> IN is OR, I want the AND logic. Select posts which has tag 1, 2 and 3 = ( tag ids ) >>=20 >> Build arrays and then use the =E2=80=9Ccontains=E2=80=9D operator. >>=20 >> David J.=20 >=20 Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.