X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 91D17329E82 for ; Wed, 22 Sep 2004 10:14:33 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 81745-02 for ; Wed, 22 Sep 2004 09:14:27 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id A474D329E45 for ; Wed, 22 Sep 2004 10:14:28 +0100 (BST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i8M9EQfg083081 for ; Wed, 22 Sep 2004 09:14:26 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i8M98b48081263 for pgsql-sql@postgresql.org; Wed, 22 Sep 2004 09:08:37 GMT From: Cris Carampa X-Newsgroups: comp.databases.postgresql.sql Subject: where clause on a left outer join Date: Wed, 22 Sep 2004 11:08:22 +0200 Organization: PYRENET Midi-pyrenees Provider Lines: 99 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@pyrenet.fr User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040114 X-Accept-Language: it, en-us To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.9 tagged_above=0.0 required=5.0 tests=FROM_ENDS_IN_NUMS, MAILTO_TO_SPAM_ADDR X-Spam-Level: * X-Archive-Number: 200409/224 X-Sequence-Number: 19070 Hello, let's suppose I have the following tables: create table parent ( parent_id numeric primary key, parent_data text ) ; create table stuff ( stuff_id numeric primary key, parent_id numeric references parent, stuff_data text ) ; And the following data: crisdb=> select * from parent; parent_id | parent_data -----------+------------- 1 | aaa 2 | bbb 3 | ccc (3 rows) crisdb=> select * from stuff; stuff_id | parent_id | staff_data ----------+-----------+------------ 1 | 1 | xxx 2 | 1 | yyy 3 | 1 | zzz (3 rows) I wish to write a query that returns all rows from "parent" and, beside of them, staff data with stuff_id=1 if available, otherwise null. The following query: select par.parent_id, stu.stuff_data from parent par left outer join stuff stu on ( par.parent_id = stu.parent_id ) where stu.stuff_id = 1 ; Gives the following result: parent_id | stuff_data -----------+------------ 1 | xxx (1 row) But this is not what I want. The following query: select par.parent_id, stu.stuff_data from parent par left outer join ( select * from stuff where stuff_id = 1 ) stu on ( par.parent_id = stu.parent_id ) ; Gives the following result: parent_id | stuff_data -----------+------------ 1 | xxx 2 | 3 | (3 rows) Which is exacly what I want. I'm wondering whether there is another way to get this result, without using the online view. Thank you. Kind regards, -- Cris Carampa (spamto:cris119@operamail.com) I got some John Coltrane on the stereo baby make it feel all right I got some fine wine in the freezer mama I know what you like I said a man works hard all day he can do what he wants to at night