www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Using JOIN in MySQL

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265

    resolved [RESOLVED] Using JOIN in MySQL

    Okay, so I've got a ton of experience using MySQL but I've never actually mastered using JOINs.

    Basically I have these:

    community:
    • id
    • title


    house:
    • lot_number
    • community (foreign key to community(id))
    • price
    • square_feet


    floor_plan:
    • title
    • square_feet


    Sort of like that, but with a lot of other tables and a lot of other fields.

    What I'm trying to do is something like this:

    Lot (lot.lot_number) at (community.title) features the (floor_plan.title) plan.

    How would you suggest I go about doing something like this in the most efficient way possible? The system has a lot of inter-connecting parts so it gets pretty complicated at times.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,636
    I would try to normalize things a bit with a couple minor tweaks to the schema:
    Code:
    community:
    ===========
        community_id
        title
    
    house:
    ===========
        house_id
        lot_number
        community_id (foreign key to community)
    	floor_plan_id (FK to floor_plan)
        price
    
    floor_plan:
    ===========
        floor_plan_id
        title
        square_feet
    Then a typical query might be along the lines of:
    Code:
    SELECT
      community.community_id,
      house.lot_number,
      house.price,
      floor_plan.title AS floor_plan_title, -- alias to avoid confusion
      floor_plan.square_feet
    FROM house
    INNER JOIN community ON house.community_id = community.community_id
    INNER JOIN floor_plan ON house.floor_plan_id = floor_plan.floor_plan_id
    WHERE community.community_id = 3 AND house.house_id = 234
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles