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 > Join three tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-15-09, 10:57
cbrasfield cbrasfield is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
Join three tables

Hello,

I have three tables: directory, child, pet
child and pet have foreign keys associated with directory (mid)

I need a sql statement that will query the tables and return the child and pet associated values in a single column. I can do this with two tables, but when I add in the third, I get duplicates. For instance, if I have one pet, two children, then I get a second returned value of pet.

Here's my sql for two that works just lovely:
Code:
select d.mid, d.lastName, d.firstName1, d.firstName2, d.address, d.phone, d.email, group_concat(c.name) as childName from child c right join directory d on d.mid = c.mid group by d.mid
My sql for three that is not quite right:
Code:
select d.mid, d.lastName, d.firstName1, d.firstName2, d.address, d.phone, d.email, group_concat(c.name) as childName, group_concat(c.age) as childAge, group_concat(p.name) as petName, group_concat(p.description) as description from child c
inner join pet p on p.mid = c.mid
right join directory d on d.mid = c.mid
group by d.mid
Any help would be appreciated, thank you!
Reply With Quote
  #2 (permalink)  
Old 06-15-09, 12:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT d.mid
     , d.lastName
     , d.firstName1
     , d.firstName2
     , d.address
     , d.phone
     , d.email
     , c.children
     , p.pets
  FROM directory AS d
LEFT OUTER
  JOIN ( SELECT mid
              , GROUP_CONCAT(
                  CONCAT(name,',',age)
                  SEPARATOR ';') AS children
           FROM child 
         GROUP
             BY mid ) AS c
    ON c.mid = d.mid
LEFT OUTER
  JOIN ( SELECT mid
              , GROUP_CONCAT(
                  CONCAT(name,',',description)
                  SEPARATOR ';') AS pets
           FROM pet 
         GROUP
             BY mid ) AS p
    ON p.mid = d.mid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-15-09, 13:07
cbrasfield cbrasfield is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
You are absolutely wonderful!! Thank you so much. I'm going to buy your book.
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