Subject: Retrieve query

From: Kacper Nowicki <[email-masked]>
Date: Mon, 6 Mar 1995 16:29:25 +0100 (MET)

Hi,

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.

Thanks in advance,
Kacper

------------------------------------------------------------------------
Kacper Nowicki                         e-mail: [email-masked]
Warsaw University                      =================================
Physics Department Computer Center
Hoza 69, 00-681 Warszawa, Poland       fax: (+48)(22)294309

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

---

From: Robert Patrick <[email-masked]>
Date: Mon, 06 Mar 1995 21:25:24 -0500

> 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 ([email-masked]) Engineering Design Research Center |
| n-dim Group                                 Carnegie Mellon University |
| World Wide Web: http://paneer.ndim.edrc.cmu.edu:8888/~rp2y/Home.html   |
+------------------------------------------------------------------------+