| | |
Joins
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
I'm confused between LEFT JOIN and INNER JOIN - the differences and when it's appropriate to use one over the other. I tried to wrap my mind around it but it seems to me that INNER JOIN statements can always be rewritten as LEFT JOINs?
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
left joins will return all rows in the initial table regardless of whether or not they have a corresponding entry in the joined table. for rows that do not have a corresponding entry, null will be returned for all selected fields
inner joins will return only the rows in the initial that have a corresponding entry in the joined table.
for example, take the following setup
table a (id1 int)
has the follwing entries
1
2
table b (id1 int)
has the following entries
1
select * from a
INNER join b on a.id1 = b.id1
will return one row
1 1
select * from a
LEFT join b on a.id1 = b.id1
will return two rows
1 1
2 NULL
inner joins will return only the rows in the initial that have a corresponding entry in the joined table.
for example, take the following setup
table a (id1 int)
has the follwing entries
1
2
table b (id1 int)
has the following entries
1
select * from a
INNER join b on a.id1 = b.id1
will return one row
1 1
select * from a
LEFT join b on a.id1 = b.id1
will return two rows
1 1
2 NULL
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
now for when you might want to do that.
Lets say you have started weekly and a monthly newsletter for this site, with the members having the option to sign up for either, neither or both.
Now let's say you wanted to get a list of all the members who signed up for the weekly list, you would use an inner join
this would return something like
1 bob bob@somewhere.com 1
2 jane jane@somewhere.com 2
and you would know that bob and jane are the only two members signed up for your weekly newsletter
Now let's say you wanted to get a list of ALL members and any lists they might be belong to. You would use a left join
this would return something like
1 bob bob@somewhere.com 1 NULL
2 jane jane@somewhere.com 2 2
3 joe joe@Somewhere.com NULL 3
4 beth beth@somewhere.com Null NULL
and you would know that you have 4 members
bob is only signed up for weekly
jane is signed up for both
joe is only signed up for monthly
beth is signed up for neither
Lets say you have started weekly and a monthly newsletter for this site, with the members having the option to sign up for either, neither or both.
Now let's say you wanted to get a list of all the members who signed up for the weekly list, you would use an inner join
MySQL Syntax (Toggle Plain Text)
SELECT Members.* FROM Members INNER JOIN WeeklyNewsletter on Members.MemberID = WeeklyNewsletter.MemberID
1 bob bob@somewhere.com 1
2 jane jane@somewhere.com 2
and you would know that bob and jane are the only two members signed up for your weekly newsletter
Now let's say you wanted to get a list of ALL members and any lists they might be belong to. You would use a left join
MySQL Syntax (Toggle Plain Text)
SELECT * FROM Members LEFT JOIN weeklynewsletter on members.memberid = weeklynewsletter.memberid LEFT JOIN monthlynewsletter on members.memberid = monthlynewsletter.memberid
this would return something like
1 bob bob@somewhere.com 1 NULL
2 jane jane@somewhere.com 2 2
3 joe joe@Somewhere.com NULL 3
4 beth beth@somewhere.com Null NULL
and you would know that you have 4 members
bob is only signed up for weekly
jane is signed up for both
joe is only signed up for monthly
beth is signed up for neither
![]() |
Similar Threads
- You Guys Play Videogames? (Geeks' Lounge)
- Wordpress, Joins and Indexes (MySQL)
- Sql Joins (MySQL)
- MS SQL Joins - newb assistance (MS SQL)
- Problem with Rewriting Subqueries as Joins (Database Design)
- Problem with Rewriting Subqueries as Joins (MS SQL)
Other Threads in the MySQL Forum
- Previous Thread: Persistent vs Non persistent db connections
- Next Thread: php & mysql
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency







