Results 1 to 3 of 3

Thread: Join Query

  1. #1
    Join Date
    Dec 2009
    Posts
    2

    Cool Unanswered: 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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Dec 2009
    Posts
    2

    Cool Thanks a Lot

    Thanks a lot. It really worked for me....Nice.....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •