SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2001
    Location
    Fort Lauderdale
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post I'm confused about joins.

    Aight i learn pretty quick once i understand the concept of something, and like what its used for.

    Now when you join tables.... why would u do this?

    How can it help you, and what can you accomplish with this??


    Thank you.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the old classic example; ducks with cars

    Let's say you have a table with ducks...
    duck_id, first_name, last_name, car_model, reg_number
    1, Donald, Duck, Belchfire Runabout, 313
    2, Grandma, Duck, Detroit Electric, 1902
    ...

    This is not a good normalized table
    If there is another duck with the same car model as, for example, Donald, the name "Belchfire Runabout" is repeated on that row (the table takes up more space and if the name is misspelt, we must update multiple rows)
    A better idea is to put the car models in a separate table...
    car_model_id, car_model
    1, Belchfire Runabout
    2, Detroit Electric
    3, Zoom V-8
    ...
    And then change the ducks table...
    duck_id, first_name, last_name, car_model_id, reg_number
    1, Donald, Duck, 1, 313
    2, Grandma, Duck, 2, 1902
    ...

    That's better
    But, what should we do if Donald wins a new car ("Lucky Number", July 20, 1951) ?
    A really stupid idea would be to add a new row in the ducks table for him
    Solution: Create a table called duck_car...
    car_id, duck_id, car_model_id, reg_number
    1, 1, 1, 313
    2, 2, 2, 1902
    3, 1, 3, unknown <= Donald's new sportscar!
    ...
    (and of course remove the car_model_id and reg_number columns from the ducks table)

    Then, when we want to query the database "Get Donald's cars", we have to join the tables:
    select dc.car_id, cm.car_model, dc.reg_number
    from duck d
    inner join duck_car dc on d.duck_id = dc.duck_id
    inner join car_model cm on dc.car_model_id = cm.car_model_id
    where d.first_name = 'Donald'

    Short answer to your question; you join tables to get related data from two or more tables

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Addict five40's Avatar
    Join Date
    Jul 2001
    Location
    Imatra, Finland
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a nice tutorial about joins by DevArticles.
    "-Surely you can't be serious ?
    -Yes I am serious...and don't call me Shirley."

  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by five40
    Here is a nice tutorial about joins by DevArticles.
    [off topic]
    The flash ad in the top left corner was kind of cool...
    [/off topic]

  6. #6
    SitePoint Guru
    Join Date
    Nov 2001
    Location
    Fort Lauderdale
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all the replies, I'm going to study this alittle more and follow the links that were posted.

    Thanx for all those that took the time to reply.


Bookmarks

Posting Permissions

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