pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Left Join 3 tables
2+ messages / 2 participants
[nested] [flat]

* Left Join 3 tables
@ 2003-05-14 17:35  Hunter <dave_h4@yahoo.com>
  0 siblings, 1 reply; 2+ messages in thread

From: dave_h4@yahoo.com @ 2003-05-14 17:35 UTC (permalink / raw)
  To: pgsql-sql

Hi Group - 

1) I have 3 tables: A, B and C
2) Each table has an unqiue associative field called cust_id
3) The cust_id HAS to be in table A, the cust_id *could* be in table
B, and the cust_id *could* be in table C
4) I need my query to: 
  a) count all rows where cust_id is in table A but *not* in table B
AND *not* in table C
  b) display all emails where cust_id is in table A but *not* in table
B AND *not* in table C

Here's some of my queries of what I have:

This one is close I think:

select distinct A.email from A
left join A as ics on ics.custid = A.custid
left join B on B.custid = A.custid
left join C on C.custid  = A.custid
where ics.custid <> A.custid;

THANK YOU!

dave



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Left Join 3 tables
@ 2003-05-15 11:39  David W Noon <dwnoon@spamtrap.ntlworld.com>
  parent: Hunter <dave_h4@yahoo.com>
  0 siblings, 0 replies; 2+ messages in thread

From: David W Noon @ 2003-05-15 11:39 UTC (permalink / raw)
  To: pgsql-sql

On Wednesday 14 May 2003 18:35 in
<27ad91d2.0305140935.4fdec226@posting.google.com>, Hunter
(dave_h4@yahoo.com) wrote:

> 4) I need my query to:
>   a) count all rows where cust_id is in table A but *not* in table B
> AND *not* in table C

SELECT COUNT(*) FROM A
   WHERE NOT EXISTS(SELECT 1 FROM B WHERE B.cust_id = A.cust_id)
     AND NOT EXISTS(SELECT 1 FROM C WHERE C.cust_id = A.cust_id);

>   b) display all emails where cust_id is in table A but *not* in table
> B AND *not* in table C

SELECT A.email FROM A
   WHERE NOT EXISTS(SELECT 1 FROM B WHERE B.cust_id = A.cust_id)
     AND NOT EXISTS(SELECT 1 FROM C WHERE C.cust_id = A.cust_id);

-- 
Regards,

Dave
======================================================
dwnoon@spamtrap.ntlworld.com (David W Noon)
Remove spam trap to reply via e-mail.
======================================================



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-05-15 11:39 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-05-14 17:35 Left Join 3 tables Hunter <dave_h4@yahoo.com>
2003-05-15 11:39 ` David W Noon <dwnoon@spamtrap.ntlworld.com>

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