Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-31-2005, 10:27 PM   #1 (permalink)
snauw
Registered User
 
Join Date: Jan 2005
Posts: 3
snauw is on a distinguished road
Left outer join problem

I have the following query:

select co.contact_id, mobilephone.PHONENR_ID, mobilephone.PHONETYPE_ENUMID
from contact co ,
contactphonenr mobilecoph left join phonenr mobilephone on
mobilephone.phonenr_id = mobilecoph.phonenr_id and mobilephone.phonetype_enumid = 4
where co.CONTACT_ID = 200057 and co.CONTACT_ID = mobilecoph.contact_id

which applies to three tables: contact, phonenr and contactphonenr which is a link table between contact
and phonenr. The phonenr can be of three types: mobile, fax or main. I want the contact with the mobile
phonenr phoneid or if not present with an empty phoneid.

This query works more or less, but if the contact has a fax and main phonenr, but no mobile phonenr
the result is two records without a phoneid in stead of one.
Something simular happens when I try to select the fax for this contact, I receive a record with the fax and
an empty record.

Can anyone help me? And tell me why I receive two records in stead of one.
snauw is offline   Reply With Quote
Old 01-31-2005, 10:40 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
should there be an 'and' in the 'on' clause? without your data schema it's hard to figure out what you're doing. .. i just tried to have a wack at it and i am totally lost with your table names and that join syntax.

what db are you using? and maybe tell a little bit more about the tables you are querying.
__________________
Mike
sde is offline   Reply With Quote
Old 01-31-2005, 11:03 PM   #3 (permalink)
snauw
Registered User
 
Join Date: Jan 2005
Posts: 3
snauw is on a distinguished road
simulate situation

Like this you can simulate the situation:

CREATE TABLE CONTACT
(
CONTACT_ID NUMBER(12) NOT NULL
);

CREATE TABLE CONTACTPHONENR
(
PHONENR_ID NUMBER(12) NOT NULL,
CONTACT_ID NUMBER(12) NOT NULL
);

CREATE TABLE PHONENR
(
PHONENR_ID NUMBER(12) NOT NULL,
PHONETYPE_ENUMID NUMBER(12) NOT NULL,
ZONALNR VARCHAR2(10 BYTE) NOT NULL,
LOCALNR VARCHAR2(50 BYTE) NOT NULL
);


INSERT INTO CONTACT ( CONTACT_ID ) VALUES (200057);
INSERT INTO CONTACT ( CONTACT_ID ) VALUES (200058);
INSERT INTO CONTACT ( CONTACT_ID ) VALUES (200059);
INSERT INTO CONTACT ( CONTACT_ID ) VALUES (200060);
COMMIT;

INSERT INTO CONTACTPHONENR (PHONENR_ID, CONTACT_ID ) VALUES (200061, 200057);
INSERT INTO CONTACTPHONENR (PHONENR_ID, CONTACT_ID ) VALUES (200062, 200057);
INSERT INTO CONTACTPHONENR (PHONENR_ID, CONTACT_ID ) VALUES (200063, 200058);
INSERT INTO CONTACTPHONENR (PHONENR_ID, CONTACT_ID ) VALUES (200064, 200059);
COMMIT;

INSERT INTO PHONENR (PHONENR_ID, PHONETYPE_ENUMID, ZONALNR, LOCALNR) VALUES (200061, 3, '061', '3193281');
INSERT INTO PHONENR (PHONENR_ID, PHONETYPE_ENUMID, ZONALNR, LOCALNR) VALUES (200061, 4, '061', '3193281');
INSERT INTO PHONENR (PHONENR_ID, PHONETYPE_ENUMID, ZONALNR, LOCALNR) VALUES (200062, 3, '061', '3193281');
INSERT INTO PHONENR (PHONENR_ID, PHONETYPE_ENUMID, ZONALNR, LOCALNR) VALUES (200063, 4, '061', '3193281');
COMMIT;

I'm using Oracle.
snauw is offline   Reply With Quote
Old 02-01-2005, 07:49 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
Code:
SELECT C.CONTACT_ID, P.PHONENR_ID, P.PHONETYPE_ENUMID
  FROM CONTACT AS C
  LEFT JOIN CONTACTPHONENR AS CP ON C.CONTACT_ID=CP.CONTACT_ID
  LEFT JOIN PHONENR AS PN ON CP.PHONENR_ID=PN.PHONENR_ID
  WHERE PN.PHONETYPE_ENUMID=4
    AND C.CONTACT_ID=20057
i'm just guessing here, but if that doesn't work, maybe add a 'GROUP BY C.CONTACT_ID'

i'm not sure. if you find the answer, i'd be interested to know what works.


from contact co ,
contactphonenr mobilecoph left join phonenr mobilephone on
mobilephone.phonenr_id = mobilecoph.phonenr_id and mobilephone. and co.CONTACT_ID = mobilecoph.contact_id
__________________
Mike
sde is offline   Reply With Quote
Old 02-01-2005, 10:39 PM   #5 (permalink)
snauw
Registered User
 
Join Date: Jan 2005
Posts: 3
snauw is on a distinguished road
solution

SELECT mobile.local, mobile.zonal
FROM
contact,
(select mobilecoph.contact_id as contact_id, max(mobilephone.PHONENR_ID), max(mobilephone.LOCALNR) as local, max(mobilephone.ZONALNR) as zonal
from contactphonenr mobilecoph, phonenr mobilephone
where mobilecoph.phonenr_id = mobilephone.phonenr_id(+)
and mobilephone.phonetype_enumid(+) = 4
group by mobilecoph.contact_id) mobile
WHERE mobile.contact_id(+) = co.CONTACT_ID
and co.CONTACT_ID = 200057
snauw is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 07:59 AM
looping problem maria_arif MS Technologies ( ASP, VB, C#, .NET ) 1 11-29-2004 08:07 AM
SQL Query Help Please sde Program Design and Methods 6 03-07-2004 10:48 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 06:17 AM


All times are GMT -8. The time now is 10:21 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting