943,945 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 1585
  • MySQL RSS
May 4th, 2006
0

Joins

Expand Post »
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?
Similar Threads
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
May 4th, 2006
0

Re: Joins

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
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 4th, 2006
0

Re: Joins

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
MySQL Syntax (Toggle Plain Text)
  1. SELECT Members.* FROM Members
  2. INNER JOIN WeeklyNewsletter on Members.MemberID = WeeklyNewsletter.MemberID
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
MySQL Syntax (Toggle Plain Text)
  1. SELECT * FROM Members
  2. LEFT JOIN weeklynewsletter on members.memberid = weeklynewsletter.memberid
  3. 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
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 4th, 2006
0

Re: Joins

Awesome, thanks That answered my question.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
May 4th, 2006
0

Re: Joins

no problem. Let me know if you need any more help with them.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Persistent vs Non persistent db connections
Next Thread in MySQL Forum Timeline: php & mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC