Results 1 to 4 of 4

Thread: how to use WHERE with two tables?

  1. #1
    Join Date
    Jan 2003
    Posts
    2

    how to use WHERE with two tables?

    I have two tables:table[games] and table[reviews].
    In table[games] I store only the game titles.
    In table[reviews] I store titles of the games with writing reviews (the titles in "reviews writing process" are taking from table[games]).
    I want to receive the titles for which I don't have the reviews - it must be the titles from table[games] different than titles from table[reviews].

    <?PHP
    echo "<TABLE><TR>";
    echo "<TD><SELECT NAME=\"title\">";
    if (pnSecAuthAction(0, 'Games::', "$title:: ", ACCESS_READ))
    $column1 = &$pntable['games_column'];
    $column2 = &$pntable['reviews_column'];
    $result = $dbconn->Execute("SELECT $column1[title], $column2[title]
    FROM $pntable[games], $pntable[reviews]
    WHERE $column1[title]<>$column2[title]
    ");
    $tmp = 0;
    while(list($title) = $result->fields)
    {
    if ($tmp == 0) {echo "<option >"._GAMESALL."</option> <option> </option>";}
    echo "<option value=\"$title\">".pnVarPrepForDisplay($title)."</option>";
    $result->MoveNext();
    $tmp = $tmp + 1;
    }
    echo "</SELECT></TD></TR></TABLE>";

    ?>

    but it don't work correctly

  2. #2
    Join Date
    Jan 2003
    Location
    FL
    Posts
    13
    I haven't done any PHP yet, but the
    logic for this is:

    select titles from A where titles NOT IN
    (select titles from B)

    which is NOT what you're doing IIRTC.
    You are asking for all titles in A
    not equal to titles in B -- that's
    almost cartesian. You should be able to
    re-write your code based on above.

    Cheers,
    Gary

  3. #3
    Join Date
    Jan 2003
    Posts
    2

    thanks

    thank you Gary, but it's probably don't work in MySQL, that this is not the solution for me

  4. #4
    Join Date
    Jan 2003
    Location
    FL
    Posts
    13
    I haven't done any MySQL work but
    http://www.mysql.com/doc/en/JOIN.html
    appears to have a join solution to your question.
    To quote:

    # If there is no matching record for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find records in a table that have no counterpart in another table:
    mysql> SELECT table1.* FROM table1
    -> LEFT JOIN table2 ON table1.id=table2.id
    -> WHERE table2.id IS NULL;
    This example finds all rows in table1 with an id value that is not present in table2 (that is, all rows in table1 with no corresponding row in table2). This assumes that table2.id is declared NOT NULL, of course.


    Cheers,
    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •