SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySql Join 3 Tables?

    Hi Everyone,

    How would I go about joining 3 tables at once? My database design/setup is at the below URL. I want the mysql statement to say "1992 Ford Explorer"

    http://whatsmyowncarworth.com/class-...se-display.php

    Thanks Everyone!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Here you can read about joins.

    If you post the query you've got, we can see where you're going wrong.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Guido2004,

    Thanks for the reply. I'm reading your link currently.

    These are my sql commands. Please advise.

    Code:
    SELECT car_years.name, car_makes.name 
    FROM car_makes, car_years 
    WHERE car_years.id=car_year_id
    
    year, make
    
    >>>>>>>
    
    SELECT car_makes.name, car_models.name 
    FROM car_makes, car_models
    WHERE car_makes.id=car_make_id
    
    make, model
    
    >>>>>>>>
    
    SELECT car_years.name, car_models.name 
    FROM car_years, car_models
    WHERE car_years.id = car_year_id
    
    SELECT years.name, models.name 
    FROM car_years AS years, car_models AS models
    WHERE years.id = car_year_id
    
    year, model

  4. #4
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're joining on something called year_id, I think something's gone very wrong!
    I would expect to see something more along these lines

    Code:
    makes(make_id*,make) 
    models(make_id*,model_id*,model) [also (`make_id`,`model`) should be unique]
    model_year(model_id*,year*)
    
    * = PRIMARY KEY (or component thereof)
    
    SELECT k.make
         , d.model
         , y.year
      FROM makes k
      JOIN models d
        ON d.make_id = k.make_id
      JOIN model_year y
        ON y.model_id = d.model_id;


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
  •