Return-Path: owner-postman
Received: from localhost.Berkeley.EDU (localhost.Berkeley.EDU [127.0.0.1]) by nobozo.CS.Berkeley.EDU (8.6.10/8.6.3) with SMTP id SAA01088 for postgres-redist; Mon, 6 Mar 1995 18:26:39 -0800
Resent-From: POSTGRES mailing list <postman@postgres.Berkeley.EDU>
Resent-Message-Id: <199503070226.SAA01088@nobozo.CS.Berkeley.EDU>
X-Authentication-Warning: nobozo.CS.Berkeley.EDU: Host localhost.Berkeley.EDU didn't use HELO protocol
Sender: owner-postman@postgres.Berkeley.EDU
X-Return-Path: owner-postman
Received: from RASAM.NDIM.EDRC.CMU.EDU (RASAM.NDIM.EDRC.CMU.EDU [128.2.214.234]) by nobozo.CS.Berkeley.EDU (8.6.10/8.6.3) with SMTP id SAA01078 for <postgres@nobozo.cs.berkeley.edu>; Mon, 6 Mar 1995 18:26:37 -0800
Message-Id: <199503070226.SAA01078@nobozo.CS.Berkeley.EDU>
Received: from LOCALHOST by RASAM.NDIM.EDRC.CMU.EDU id aa02967;
          6 Mar 95 21:25 EST
X-Mailer: exmh version 1.6alpha 2/16/95
To: postgres@postgres.Berkeley.EDU
Subject: Re: retrieve query
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Date: Mon, 06 Mar 1995 21:25:24 -0500
From: Robert Patrick <Robert_Patrick@RASAM.NDIM.EDRC.CMU.EDU>
Resent-To: postgres-redist@postgres.Berkeley.EDU
Resent-Date: Mon, 06 Mar 95 18:26:39 -0800
Resent-XMts: smtp

> I have two classes: one with info about institutions and second with their
> phone numbers (and with institution id). How should I write query which
> returns institution name and phone number ? I need name even if there is
> no phone number for it. Simple : 

> retrieve (i.name, ph.number) where i.id=ph.id

> returns only those instances from i which has matching entry in ph.

Actually, the only way to do this without restructuring your tables is to 
define a function.  Here's how you would do it:

 rasam.ndim.edrc.cmu.edu> monitor rp2y_test
Welcome to the POSTGRES terminal monitor

Go 
* create info(id=int4, name=text)\g

Query sent to backend is "create info(id=int4, name=text)"
CREATE
Go 
* create phone(id=int4, number=text)\g

Query sent to backend is "create phone(id=int4, number=text)"
CREATE
Go 
* define function getPhoneNumber
        (language = "postquel", returntype = text)
        arg is (int4) 
        as "retrieve (phone.number) where phone.id = $1"\g

Query sent to backend is "define function getPhoneNumber        (language = 
"postquel", returntype = text)      arg is (int4)   as "retrieve 
(phone.number) where phone.id = $1""
DEFINE
Go 
* append info( id = 1, name="CMU")\g

Query sent to backend is "append info( id = 1, name="CMU")"
APPEND 38080
Go 
* append info(id=2, name = "MIT")\g

Query sent to backend is "append info(id=2, name = "MIT")"
APPEND 38081
Go 
* append phone(id =2, number="234-5678")\g

Query sent to backend is "append phone(id =2, number="234-5678")"
APPEND 38082
Go 
*  retrieve(info.id, num = getPhoneNumber(info.id))\g

Query sent to backend is "retrieve(info.id, num = getPhoneNumber(info.id))"
-----------------------------
| id          | num         |
-----------------------------
| 1           |             |
-----------------------------
| 2           | 234-5678    |
-----------------------------

Go 
* 


Robert

--
+------------------------------------------------------------------------+
| Robert Patrick (rp2y+@edrc.cmu.edu) Engineering Design Research Center |
| n-dim Group                                 Carnegie Mellon University |
| World Wide Web: http://paneer.ndim.edrc.cmu.edu:8888/~rp2y/Home.html   |
+------------------------------------------------------------------------+













==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU".
   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
              URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/
