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 1g074H-0003ne-OX for pgsql-general@arkaria.postgresql.org; Wed, 12 Sep 2018 15:28:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g074G-0003rx-82 for pgsql-general@arkaria.postgresql.org; Wed, 12 Sep 2018 15:28:36 +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 1g074G-0003rq-0P for pgsql-general@lists.postgresql.org; Wed, 12 Sep 2018 15:28:36 +0000 Received: from mail-oi0-x22e.google.com ([2607:f8b0:4003:c06::22e]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g074C-0005h6-Oh for pgsql-general@lists.postgresql.org; Wed, 12 Sep 2018 15:28:35 +0000 Received: by mail-oi0-x22e.google.com with SMTP id t68-v6so4480044oie.12 for ; Wed, 12 Sep 2018 08:28:32 -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=isWO/3o2ACgwglVFSRJuSCGXMFXeHYb7WT8z5w5G+LQ=; b=XhTAvcTuEDTul4y5tU24GNbrmPPw5b+DI7n7oEQb4eYJZpr5rI5CkzyWJoT4usQxwV u8zF8CfHyKzKUCDv3a+7SjV8SC6wbwcwgBXK8SRaE5M8YbGmeEDsW3qmyiy5KnvVQndt ng564jEZ2WJMZIxz9EWOS/ITq/BXzuPQgLv3Cdu/DDOVI2SPDD8WFUz9+Ug37qVOehDn FEuxck9IFzLc+lu3r+GYuQUi+f0yNL3dd8tvZ3ZGExZCp/5NkwXoapN1Ivo8F1YWkHh+ ajkfolIL3whnGjNwPb0NXF906ivU4ulgiDprr43o+hk61L+pODOufAXPU1YVPjYD8mMN qVsQ== 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=isWO/3o2ACgwglVFSRJuSCGXMFXeHYb7WT8z5w5G+LQ=; b=A8cTEpXcWIwJfFMKVMAiYbKGCgboB/4t4IyD+2BIXh6AatP9f8Pd6Ya0Jd67YbRco+ EElWbcAQeCnIQYQHRNoyVXOlQjIygyXsc2rxupXMq4wEi9+8QHWSH4d7B75/JqI0dCnG EaEx+ZUVwQwtrAZbgK68kkVjBnTlAVi2r82illMLfdQYL9QJ1XQILdEUy//TYyssEWNg R9s59ubkd30LV+6KQnji+8vEpdCJwDYQOA4I6d1d0raYQKnJNFcALMF2lsGyrlA25w19 xtQ89YfRo03orXpOF6Q1qoXrYyidbpRYVVQUq1WTKMhMldSU/BOaiMJbNJ+uxzo4Z/+C AVzw== X-Gm-Message-State: APzg51BSm1leLbsWM3T7BbTKOgsmhtCPGPV/suXkEnpGxL8tuOtm7ZgQ zfLs9qydXOOrfJChn3WOdtEA5Yib X-Google-Smtp-Source: ANB0VdZBsfDdez1KfRxlFAa3Vf43I2xqOD5w25zAr2DtJNoQOfZ5/TVkQYcO12X08umqgO3ho5voFw== X-Received: by 2002:aca:100f:: with SMTP id 15-v6mr2378694oiq.110.1536766109463; Wed, 12 Sep 2018 08:28:29 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id w13-v6sm1179303oiw.51.2018.09.12.08.28.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Sep 2018 08:28:28 -0700 (PDT) Subject: Re: Select rows when all all ids of its children records matches To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: Date: Wed, 12 Sep 2018 10:28:27 -0500 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="------------17EB5092B1B4B886542FCE66" 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. --------------17EB5092B1B4B886542FCE66 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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 > > > -- Angular momentum makes the world go 'round. --------------17EB5092B1B4B886542FCE66 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit 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




--
Angular momentum makes the world go 'round.
--------------17EB5092B1B4B886542FCE66--