Tuesday, August 30, 2011

Select from multiple tables in MySQL


Below is the example using full join and left join in MySQL.

Suppose we have two tables: members and committee.
table members (primary key member_id)
--------------------------------------
member_id       member_name
1                         Joe
2                         Andy
3                        Tommy

table committee (primary key committee_id)
----------------------------------------------
committee_id  member_id  committee_name  
1                           2                   Research                
2                           2                  Education               
3                          3                    Research               
Full join
SELECT  members.*, committee.*  FROM members, committee;
Result:
------------------------------------------------------------------------------------------------------
member_id       member_name  committee_id  member_id  committee_name 
1                         Joe             1                           2                   Research      
2                         Andy                     1                           2                   Research      
3                        Tommy                  1                           2                   Research      
1                         Joe                       2                           2                  Education             
2                         Andy                     2                           2                  Education             
3                        Tommy                   2                           2                  Education             
1                         Joe                        3                          3                    Research    
2                         Andy                     3                           3                    Research 
3                        Tommy                  3                           3                    Research 

SELECT  members.*, committee.*  FROM members, committee WHERE members.committee_id=committee.committee_id;
Result:
------------------------------------------------------------------------------------------------------
member_id       member_name  committee_id  member_id  committee_name  
 2                         Andy                     1                           2                   Research      
2                         Andy                     2                           2                  Education             
3                        Tommy                  3                           3                    Research 

Left join
SELECT members.*, committee.*  from members LEFT JOIN committee ON members.committee_id=committee.committee_id;
Result:
(A left join produces output for every row in members, whether or not  committee matches it. )
------------------------------------------------------------------------------------------------------
member_id       member_name  committee_id  member_id  committee_name
 1                         Joe                        NULL                NULL               NULL
 2                         Andy                     1                           2                   Research      
2                         Andy                     2                           2                  Education             
3                        Tommy                  3                           3                    Research 

No comments:

Post a Comment