Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id E6A2C2E2D42 for ; Wed, 14 Nov 2007 11:46:38 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 69898-09 for ; Wed, 14 Nov 2007 11:46:26 -0400 (AST) Received: from smtp110.mail.mud.yahoo.com (smtp110.mail.mud.yahoo.com [209.191.85.220]) by postgresql.org (Postfix) with SMTP id B57DD2E2D22 for ; Wed, 14 Nov 2007 11:46:32 -0400 (AST) Received: (qmail 48863 invoked from network); 14 Nov 2007 15:46:31 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.br; h=Received:X-YMail-OSG:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=SjalnqHT/7r4Vv/PRlg5RGB1jjIB9DhsP9k6igAA/flsrFFobDycK1Y6PCfuCNEVcg98SzF4yvBm2OS8jyPV9Su388EwokXhp7cg1JD8Pdxn1TUirZ/IWix03gFEvcM5zllKhAnb4mLHbrPwWvQyiLRzg/Kb3Ov9LRrslMoRHtw= ; Received: from unknown (HELO ?10.2.200.151?) (osvaldo_kussama@189.25.166.18 with plain) by smtp110.mail.mud.yahoo.com with SMTP; 14 Nov 2007 15:46:28 -0000 X-YMail-OSG: fvIe94wVM1kG7_l4Nbn9kTM8G81UVXQQFUrN2c_Uxvz5vBgOO.moUfI5cSsQRH.LkI0E7SOzSA-- Message-ID: <473B185B.2050502@yahoo.com.br> Date: Wed, 14 Nov 2007 13:46:35 -0200 From: Osvaldo Rosario Kussama User-Agent: Thunderbird 1.5.0.12 (X11/20070530) MIME-Version: 1.0 To: Julien Cigar CC: pgsql-sql@postgresql.org Subject: Re: ALL() question References: <1195047542.3190.9.camel@frodon.be-bif.ulb.ac.be> In-Reply-To: <1195047542.3190.9.camel@frodon.be-bif.ulb.ac.be> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/72 X-Sequence-Number: 29600 Julien Cigar escreveu: > Hello, > > I have a problem with the ALL() subquery expression. > I have three tables: > - specimens > - test_bits > - specimen_test_bits > > The specimen_test_bits table contains two foreign keys, one to > specimens(id), another to test_bits(id). > > Here is an output of specimen_test_bits: > > muridae=> select * from specimen_test_bits; > specimen_id | test_bit_id > -------------+------------- > 46096 | 1 > 46096 | 2 > 46096 | 3 > 46096 | 4 > 52894 | 1 > 52894 | 3 > 12546 | 2 > > What I would like is a query that returns all the specimen_id of > this table which have _all_ the given test_bit_id. So in this > case, with test_bit_id 1,2,3,4 it should return only > specimen_id 46096. > > With the following I got a syntax error: > select specimen_id > from specimen_test_bits > where test_bit_id = all(1,2,3,4); > > The following works but no rows are returned : > select specimen_id > from specimen_test_bits > where test_bit_id = all(select id from test_bits where id in (1,2,3,4)); > > Any idea how I could do this ? I guess the problem is my ALL() expression ... > Unclear, but works... SELECT DISTINCT stb.specimen_id FROM specimen_test_bits stb WHERE NOT EXISTS (SELECT * FROM (VALUES (1), (2), (3) , (4)) AS foo(id) WHERE NOT EXISTS (SELECT stb1.test_bit_id FROM specimen_test_bits stb1 WHERE foo.id = stb1.test_bit_id AND stb.specimen_id = stb1.specimen_id)); Osvaldo