If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > 1:N query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-05, 07:20
rexselin rexselin is offline
Registered User
 
Join Date: Aug 2003
Location: India
Posts: 109
1:N query help

I have two tables which form a 1:many relationship.

For example,
The main table A can be:

1 ParentA graduate
2 ParentB doctor
3 ParentC engineer

and Child table can be

1 1 ChildA1 6th grade
2 1 ChildA2 graduate
3 1 ChildA3 engineer
4 2 ChildB1 doctor

Now you see, that parentA has three childs, parentB has 1 and parentC has none.

I would like to know those parents who are engineers or whose children might be engineers.

So I tried a query like
Quote:
select * from parents left join child on parents.parentid=child.parentid
where (parents.degree='engineer' or child.degree='engineer')
group by child.parentid
But this query is leaving out the parent who doesn't have a child. How do I do this?
Reply With Quote
  #2 (permalink)  
Old 11-13-05, 23:42
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
I believe you should be using a LEFT OUTER JOIN.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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