need mysql join structure help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    need mysql join structure help

    This is mySQL 101. I know it's supposed to be super simple, but I'm an
    idiot. I can't get my join to work (1st time I've tried doing joins.)

    I've read http://www.mysql.com/doc/en/JOIN.html, but I'm just not
    groking it.
    When I try to do the same thing, I can't "convert" one field into the data
    from another.

    Here's what I have. To test this concept I made two tables on a database
    called db_join:

    table: tbl_a
    id a_name
    -----------------
    1 DnD
    2 Spycraft
    3 SG-1
    4 Fading Suns
    -----------------
    -----------------
    table: tbl_b
    id game
    -----------------
    1 2
    2 1
    3 4
    4 3
    5 2
    6 1
    7 4
    8 3
    9 2
    -----------------
    -----------------

    And here's the latest attempt at generating results:

    $sql = 'SELECT tbl_b.id,game'
    . ' FROM tbl_b'
    . ' LEFT JOIN tbl_a ON game = a_name LIMIT 0, 30';
    $RS = @mysql_query($s ql, $connection) or die("Couldn't query: " .
    mysql_error());
    while ($row_RS = mysql_fetch_arr ay($RS)) {
    $block .= $row_RS['id']." - ".$row_RS['game']."<br>";
    }

    And the best I can do, moving fields and items around, is the
    following echo of the $block:

    1 - 2
    2 - 1
    3 - 4
    4 - 3
    5 - 2
    6 - 1
    7 - 4
    8 - 3
    9 - 2

    What I want the final output to be is resembling this:

    1 - Spycraft
    2 - DnD
    3 - Fading Suns
    4 - SG-1
    5 - Spycraft
    6 - DnD
    7 - Fading Suns
    8 - SG-1
    9 - Spycraft

    What am I doing wrong? I just don't get it. If I can just get a
    pointer, like "the broblem is in your WHILE statement," or "the
    problem is in the SELECT part" or something like that, at least it
    would narrow it down for me to figure it out.

    I appreciate any advice!!
    Thanks!
    Liam
  • Henk Burgstra

    #2
    Re: need mysql join structure help

    >[color=blue]
    > What am I doing wrong? I just don't get it. If I can just get a
    > pointer, like "the broblem is in your WHILE statement," or "the
    > problem is in the SELECT part" or something like that, at least it
    > would narrow it down for me to figure it out.
    >[/color]

    Hi Liam,

    You're almost there!
    Here's a pointer: you want a_name to appear in the result, yet you don't
    SELECT it. Try selecting a_name instead of game.

    Good luck,
    Henk Burgstra

    Comment

    Working...