Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 091D99F952F for ; Tue, 11 Sep 2007 18:32:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 07502-04 for ; Tue, 11 Sep 2007 18:32:27 -0300 (ADT) X-Greylist: delayed 00:05:07.43969 by SQLgrey-1.7.5 Received: from smtp-vbr10.xs4all.nl (smtp-vbr10.xs4all.nl [194.109.24.30]) by postgresql.org (Postfix) with ESMTP id 39FB89F9452 for ; Tue, 11 Sep 2007 18:32:31 -0300 (ADT) Received: from [192.168.10.66] (f107074.upc-f.chello.nl [80.56.107.74]) (authenticated bits=0) by smtp-vbr10.xs4all.nl (8.13.8/8.13.8) with ESMTP id l8BLRMUx005230 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) for ; Tue, 11 Sep 2007 23:27:23 +0200 (CEST) (envelope-from koen@madebysofa.com) Mime-Version: 1.0 (Apple Message framework v752.3) Content-Transfer-Encoding: 7bit Message-Id: <2C0EA394-5AC8-4FC8-99F3-5FAEE9720360@madebysofa.com> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-sql@postgresql.org From: Koen Bok Subject: Simple Query? Date: Tue, 11 Sep 2007 23:27:19 +0200 X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: by XS4ALL Virus Scanner X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/83 X-Sequence-Number: 29296 I am doing some optimization on our search, but I need some advise... table: item id name -------------------------------------- 1 iPod 2 Zune 3 Walkman table: search_item id_search id_item -------------------------------------- 1 1 1 2 1 3 2 2 2 3 3 1 3 3 Now what I want to have is the items that match with id_search 1 and 2 and 3. Therefore I use the following SQL query. SELECT * FROM item WHERE id IN (SELECT id_item FROM search_item WHERE id_search=1 AND id_item IN (SELECT id_item FROM search_item WHERE id_search=2 AND id_item IN (SELECT id_item FROM search_item WHERE id_search=3))); This should only return id_item 3. Would this be the best SQL query to get this result? I have the feeling there should be something better, but I cannot find it. Anyone has a hint?