Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f89DX0784928 for ; Sun, 9 Sep 2001 09:33:00 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f89D59j42087 for pgsql-general@postgresql.org; Sun, 9 Sep 2001 09:05:09 -0400 (EDT) (envelope-from news) From: Ian Barwick X-Newsgroups: comp.databases.postgresql.general, comp.databases.postgresql.questions Subject: Re: query help Followup-To: comp.databases.postgresql.general, comp.databases.postgresql.questions Date: Sun, 9 Sep 2001 15:07:13 +0200 Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 68 Message-ID: <9nfpe2$192d$1@news.tht.net> References: <200109071543.f87FhNI29963@sonic.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8Bit X-Complaints-To: scrappy@hub.org User-Agent: KNode/0.3.2 To: pgsql-general@postgresql.org X-Archive-Number: 200109/495 X-Sequence-Number: 15156 Jeff Patterson wrote: > This seems like such a basic function that I'm sure I am missing something > fundamental. I have a table, say xref, whose columns are primary key > values for other tables. Some of theses keys may be NULL for a given row > in xref. I want to create a query that returns the corresponding entries > in the other tables. > > xref: > drug_id | function_id|syst_id |text_id > -------------------------------------- > d0001 | 2 | 3 | 3423 > d0001 | 5 | | 5678 > d0056 | 3 | 5 | > > system_id: > syst_id | syst_desc > ------------------- > 3 | renal > 4 | hepatic > 5 | respiratory > > clinical_text: > text_id| clinical_text > ----------------------------------- > 3423 | 'some medical mumbo jumbo' > 5678 | 'more of the same' > > I want the syst_desc and clinical_text (plus other similar data from > tables not shown) given a drug_id and function_id. > > Any help? If I understand you correctly, you want the query to return the syst_desc and clinical_text fields where either or both are available? (and not just where both are available)? If so a LEFT JOIN may be your best friend. The statement could look like this: SELECT system_id.syst_desc, clinical_text.clinical_text FROM xref LEFT JOIN system_id ON xref.syst_id=system_id.syst_id LEFT JOIN clinical_text ON xref.text_id=clinical_text.text_id WHERE xref.drug_id=? <- insert query value here AND xref.function_id=? <- and here (disclaimer: statement untested) The values not available will be returned as NULL. > Thanks a bunch, a bunch of what? ;-) HTH Ian Barwick -- Ian Barwick - Developer http://www.akademie.de Remove SUNGLASSES to reply ;-)