SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast megler's Avatar
    Join Date
    May 2006
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing Mysql many to many query

    Dreamweaver is making me NUTS. I'm just learning MySql and PHP to begin with and am working through 3 different books to learn this. I'm stuck on how to construct the SQL query for my database.

    1. I have a premade database of baseball stats (thankfully, that should mean it's all put together correctly!)

    2. I'm trying to put together a page that, for now, will just query the db and give me results.

    I can do a simple string - show me data from 1 table. Everything works great. But if I ask for data from more than 1 table, it either times out or says my "server is shutting down" (I have optimized and repaired the db), etc.

    How to you phrase a multi table query?

    My tables are:

    Master (fields include: playerID, hofID, firstname, lastname, etc)
    HOF (hofID, yearID, votedby, ballots, votes, inducted, etc)

    So if I wanted to show the players name and if they were inducted, how do I SQL that? I think I've tried every possible combination, but I must be missing something..

    Any help is much appreciated. Sorry for the uber noob question.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    HOF is "hall of fame" ??

    if the player (Master) has been inducted into the hall of fame, the Master.hofID will have a value, otherwise it will be NULL?

    perhaps you could show a few sample rows from each table, for an inducted player and a non-inducted player
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast megler's Avatar
    Join Date
    May 2006
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem. Here are the first 10 records of the master table and the hall of fame table:

    http://baseball.completesources.com/

    I can't figure out how to make the tables show up cleanly here, all I could get was the code to print out. I have to get to work, so if you need a screen print, I can do tonight.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT m.playerID
         , m.firstname
         , m.lastname
         , h.inducted
      FROM Master AS m
    LEFT OUTER
      JOIN HOF AS h
        ON h.hofID = m.hofID
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast megler's Avatar
    Join Date
    May 2006
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r937,

    Thanks so much! Since DW won't do anything this complex, I want to understand that statement so I can continue to recreate it.

    I understand the SELECT and the FROM. what does the LEFT OUTER mean?

    I understand the JOIN - you are joining the related table to the MASTER, yes? And ON shows where the relationship is? (ie. h.hofID = m.hofID)

    thanks again for everything!

    I hope from here this is all I need to create a dynamic site with DW (as it seems to do all the rest...)

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by megler View Post
    what does the LEFT OUTER mean?
    here's chapter 3 from my book, which explains joins

    http://articles.sitepoint.com/articl...he-from-clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Enthusiast megler's Avatar
    Join Date
    May 2006
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMG, my head's going to explode! Thanks for the link. I now understand the from clause and the INNER/OUTER statements. :-)


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
  •