pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dmitri Colebatch <dim@bigpond.net.au>
To: pgsql-sql@postgresql.org
Subject: join question - three tables, two with foreign keys to the first
Date: Fri, 14 Jun 2002 00:13:22 +1000
Message-ID: <08af01c212e4$7a12a180$fe00a8c0@hobbes> (raw)

hey all,

here's my situation.  three tables:

create table employee (id integer primary key, name varchar(32));
create table employee_leave (id integer primary key, employee_id integer,
from_date date, to_date date, constraint emp_leave_fk foreign key
(employee_id) references employee (id));
create table employee_pay (id integer primary key, employee_id integer,
amount integer, constraint emp_pay_fk foreign key (employee_id) references
employee (id));

and some sample data:

insert into employee (id, name) values (1, 'dim');
insert into employee_leave (id, employee_id, from_date, to_date) values (2,
1, '10-05-2002', '14-05-2002');
insert into employee_leave (id, employee_id, from_date, to_date) values (1,
1, '10-06-2002', '14-06-2002');
insert into employee_pay(id, employee_id, amount) values (1, 1, 100);
insert into employee_pay(id, employee_id, amount) values (2, 1, 100);

and I want to retrieve the information for an employee (all pay, and all
leave) in one query....   here's what I've got

select emp.name, lv.from_date, lv.to_date, pay.amount
from employee as emp
left outer join employee_leave as lv on emp.id = lv.employee_id
left outer join employee_pay as pay on emp.id = pay.employee_id
where emp.id = 1

problem is that I dont get the null values I expect.... I want to be able to
iterate through the resultset and determine if the record is from the leave
table or pay table - but because I dont get null results, I cant....

any pointers/suggestions would be welcome.

cheers
dim





view thread (5+ messages)  latest in thread

Message-ID: <08af01c212e4$7a12a180$fe00a8c0@hobbes>
Permalink:  ../08af01c212e4$7a12a180$fe00a8c0@hobbes/
Also on:    postgresql.org/message-id/08af01c212e4$7a12a180$fe00a8c0@hobbes

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dim@bigpond.net.au
  Subject: Re: join question - three tables, two with foreign keys to the first
  In-Reply-To: <08af01c212e4$7a12a180$fe00a8c0@hobbes>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox