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 1g07ot-0006Km-C7 for pgsql-general@arkaria.postgresql.org; Wed, 12 Sep 2018 16:16:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g07or-0008Hk-SZ for pgsql-general@arkaria.postgresql.org; Wed, 12 Sep 2018 16:16:45 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g07or-0008Hd-Dv for pgsql-general@lists.postgresql.org; Wed, 12 Sep 2018 16:16:45 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g07oo-0000D5-PD for pgsql-general@lists.postgresql.org; Wed, 12 Sep 2018 16:16:44 +0000 Received: by mail-pg1-x52a.google.com with SMTP id d1-v6so1313881pgo.3 for ; Wed, 12 Sep 2018 09:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=zgJTsCYcXTN+PkVuLERY07mR90dQlwrAnJ8u48VMCB0=; b=XB2GsJhvBf4O+C7bUT7Rg6joUD8heWXKw6VCb/BN6o4EXtKAWKYHd7x1BBO87FyKWO srvJ27LPdoLYi8rcaA0SwqE5pkISbIsxWTdv3LCIz2kkihk2gVjbZxP45aYSc6s2wOBn MuUtxqKvJAPcclcZdjC58nPcUR+W2S2vTq+YPsgympxmtL3kxzuOGawl72jm1dP93jEY XUlGUo+rw8wmOodnIOFiuH87fXlWfgIqUcE2TxZIbxQZYzK6Ghoi2UtI5QWuOwRj31ib ZMM/VTL3q7JcCK//8UTZIhvTtcIf1aaoYMz6CTS9sz8EbAKpg+njDBDBijJJ63yR2EoP PsGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=zgJTsCYcXTN+PkVuLERY07mR90dQlwrAnJ8u48VMCB0=; b=YOb+ClYkkpMHZqdAdqMippNjAqsrpxiR6Rs3GBevBct0xwtHBHUl+3fRRWOrVSfIw2 jdiSPmtzVGFZ7wEPh3PwA4lB4mAxXiYxvLYh+wQSSdiUU3YUBKikob2GaDFgfH59uvLX fRZuo5MBTiXuD7syMlfzU4W4CiyRoaR5FEceW+GDxxGTMI0gGy7zLJnLTfVUAxalPGZf hLgRxTJI3Ryyo02+rTWDfxJf7aIpCrBqi5kdo4bF/HzgKOYXoFOcxQsi3OVCjnxm9cKC Az8D7NaGIZ5NdP7X3vXaz40uivDAMXuf6EPwM1eZCf4zz3k3iTU8s5k4flLc1q4SJItH 924w== X-Gm-Message-State: APzg51ATc4qvmA/fXfctmU9Ixdy6U9GdcgRivI57HPVbHQ1b6jfg+vE/ FUza50VUhxbSvBpVyNszPNIXAVHXo+k= X-Google-Smtp-Source: ANB0VdbngyKm6nnJiIet+uHCSwORlZJGCMk+D50O+9Bp9lUJhQPVbyRn28vbHIiykQryqeNM0TZnSA== X-Received: by 2002:a62:da0b:: with SMTP id c11-v6mr3256742pfh.68.1536769000819; Wed, 12 Sep 2018 09:16:40 -0700 (PDT) Received: from [10.104.134.14] ([155.100.47.6]) by smtp.gmail.com with ESMTPSA id s195-v6sm6783655pgs.76.2018.09.12.09.16.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Sep 2018 09:16:40 -0700 (PDT) Subject: Re: Select rows when all all ids of its children records matches To: pgsql-general@lists.postgresql.org References: <5E56CBFF-62E6-4C17-B6D8-BBC05BC10C8B@zeit.io> From: Rob Sargent Message-ID: Date: Wed, 12 Sep 2018 10:17:54 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------DFC25F853DD68FE6773F1DD1" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------DFC25F853DD68FE6773F1DD1 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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 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. > > Aren't you looking for cte.tags = array[1,2]? --------------DFC25F853DD68FE6773F1DD1 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit



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


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

--------------DFC25F853DD68FE6773F1DD1--