Results 1 to 5 of 5

Thread: easy SQL question

  1. #1
    Join Date
    Jan 2005
    Beans
    346

    easy SQL question

    i'm still learning SQL and i can't find if there's a better way to do this. i have two tables

    Code:
    Batting
    playerID    G    H   AB ....
    "abcde01"  10    3    32
    
    Master
    playerID .... nameFirst  nameLast
    "abcde01" ..  "abc"       "defgh"
    what i want to do is select stuff from the batting table, but i need to get the player's first and last names from the master table, using the playerID i got from the first table. right now i'm just using 2 select stmts, but i think there should be a better way to do this??

    in other words instead of printing
    abcde01 10 3 32

    i want to say
    defgh, abc 10 3 32

    without having to do 2 selects

    thx
    Last edited by jerome bettis; February 28th, 2006 at 09:10 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    Dunedin, NZ
    Beans
    559
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: easy SQL question

    Code:
    select m.nameFirst, m.nameLast, b.g, b.h, b.ab
    from Master m, Batting b
    where m.playerID = b.playerID
    ACCU - for programmers who care

  3. #3
    Join Date
    Mar 2005
    Location
    Dunedin, NZ
    Beans
    559
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: easy SQL question

    Oh, and you can easily add extra bits to the where clause to further reduce your result set.
    ACCU - for programmers who care

  4. #4
    Join Date
    Jan 2005
    Beans
    346

    Re: easy SQL question

    thanks that's perfect!

  5. #5
    Join Date
    Apr 2005
    Location
    Brazil
    Beans
    479
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: easy SQL question

    You can also use a simple JOIN:

    Code:
    select m.nameFirst, m.nameLast, b.g, b.h, b.ab
    from Master m
    JOIN Batting b ON (m.playerID = b.playerID)
    This is simple, but you can take some time a read a little about join, for writing big queries using "where" makes it kinda hard to read, but thats not the case on yours

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
  •