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 Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-09-09, 09:20
babydeveloper babydeveloper is offline
Registered User
 
Join Date: Dec 2009
Posts: 2
Cool Join Query

Hi,

I am new to mysql. I Have made a small data base and i need some help for joins.

There are three tables
1. user
user_id (pk)
user_name
user_address

2. contact
contact_id(pk)
contact_name

3. user_contact
user_id(pk)
contat_id(pk)

Now u can see there exists many to many relationship between user and contact table that is one user can have many contacts and one contact can be in the contact list of many users; so for normalization i made a junction table with name user_contact

The problem is i want to fetch username and its all contacts from contact table but i do not how to join these three tables to get data from user table and contact table. Can any write the join query that can solve the problem.
Reply With Quote
  #2 (permalink)  
Old 12-09-09, 10:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT u.user_id
     , u.user_name
     , u.user_address
     , c.contact_id
     , c.contact_name
  FROM user AS u
INNER
  JOIN user_contact AS uc
    ON uc.user_id = u.user_id
INNER
  JOIN contact AS c
    ON c.contact_id = uc.contact_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-09-09, 23:50
babydeveloper babydeveloper is offline
Registered User
 
Join Date: Dec 2009
Posts: 2
Cool Thanks a Lot

Thanks a lot. It really worked for me....Nice.....
Reply With Quote
Reply

Tags
joins, sql developer

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