Bluehost.com Web Hosting $6.95

Inner Join Query

This is a discussion on Inner Join Query within the MySQL Database forums, part of the Database Forums category; I'm having difficulty figuring out how Inner Join's work. I have read some online tutorials, but have not ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-02-2006
Greg Scharlemann
 
Posts: n/a
Default Inner Join Query

I'm having difficulty figuring out how Inner Join's work. I have read
some online tutorials, but have not been able to craft the inner join
query that I need.

I have a table: PersonLocale that contains multiple City/State entries
for each PersonID:


PersonID | City | State
1 | Charlotte | NC
1 | Raleigh | NC
1 | Durham | NC
2 | Chicago | IL
2 | Memphis | TN


I'm trying to write a query that will count the number of entries per
state. So the resulting query would give me:

NC | 3
IL | 1
TN | 1

I'll need to compare this result to another table, but would like to
figure out how to do part 1 before looking at part 2.

Thanks for your help

Reply With Quote
  #2 (permalink)  
Old 12-02-2006
Nicholas Sherlock
 
Posts: n/a
Default Re: Inner Join Query

Greg Scharlemann wrote:
> PersonID | City | State
> 1 | Charlotte | NC
> 1 | Raleigh | NC
> 1 | Durham | NC
> 2 | Chicago | IL
> 2 | Memphis | TN
>
>
> I'm trying to write a query that will count the number of entries per
> state. So the resulting query would give me:
>
> NC | 3
> IL | 1
> TN | 1
>
> I'll need to compare this result to another table, but would like to
> figure out how to do part 1 before looking at part 2.


IIRC,

SELECT State, COUNT(PersonID) FROM PersonLocale GROUP BY State

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
Reply With Quote
  #3 (permalink)  
Old 12-02-2006
Greg Scharlemann
 
Posts: n/a
Default Re: Inner Join Query


On Dec 1, 10:44 pm, Nicholas Sherlock <N.sherl...@gmail.com> wrote:
>
> SELECT State, COUNT(PersonID) FROM PersonLocale GROUP BY State
>


Wow, I was making it much harder than it needed to be. Let's suppose
there's a second table Transaction. The Transaction table stores all of
the Persons transaction information. Person can have multiple
transactions, but is not required to have a Transaction.

Transaction Table:
TransactionID | PersonID | Details
1 | 1 ...
2 | 1
3 | 3


PersonID | City | State
1 | Charlotte | NC
1 | Raleigh | NC
1 | Durham | NC
2 | Chicago | IL
2 | Memphis | TN
3 | New York | NY

The resulting table should only include the total number of Persons who
have completed a transaction in the given state:

NC | 1
NY | 1

I guess this would require some sort of Join to join the PersonID in
the PersonLocale table with the PersonID in the transaction table?
Thanks for the direction!

Reply With Quote
  #4 (permalink)  
Old 12-02-2006
Nicholas Sherlock
 
Posts: n/a
Default Re: Inner Join Query

Greg Scharlemann wrote:
> Transaction Table:
> TransactionID | PersonID | Details
> 1 | 1 ...
> 2 | 1
> 3 | 3
>
>
> PersonID | City | State
> 1 | Charlotte | NC
> 1 | Raleigh | NC
> 1 | Durham | NC
> 2 | Chicago | IL
> 2 | Memphis | TN
> 3 | New York | NY
>
> The resulting table should only include the total number of Persons who
> have completed a transaction in the given state:
>
> NC | 1
> NY | 1


Maybe:

SELECT State, COUNT(DISTINCT PersonLocale.PersonID) FROM PersonLocale
INNER JOIN TransactionTable ON
(PersonLocale.PersonID=TransactionTable.PersonID) GROUP BY State

But with your schema, you wouldn't know which state the transaction was
completed in if a person is linked to multiple states.

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:09 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0