Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    303

    MYSQL Query - Compare two tables and output the difference

    I have two tables with similar data. One table holds the same as the other plus more. Example:




    Table 1.

    1. Apples
    2. Oranges
    3. Pineapples

    Table 2.

    1. Apples
    2. Oranges
    3. Pineapples
    4. Strawberries


    How can I make it so that I run a query and the output is. "Strawberries" like the opposite of JOIN.

    I tried something but all I got was a bannana split

  2. #2
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    303
    I tried the following but it didn't work. It returns the last HTML content only, but no records.

    PHP Code:
    <?php
    // Connects to your Database 
    require_once('../connection.php');
     
    $query "SELECT budget2011data.* FROM budget2011data 
    LEFT JOIN budget ON budget.Budget = budget2011data.Budget
    WHERE budget2011data.Budget IS NULL"
    ;
     
    if (
    $r mysql_query ($query)) { // Run the query.
     
    while ($row mysql_fetch_array ($r)) {
    $Budget $row['Budget'];
     
    echo 
    "Different " $Budget "<br>";
        } 
    }
    ?>

  3. #3
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    303
    Ok the following worked. I think I had the tables stated backwards.

    PHP Code:
    <?php
    // Connects to your Database 
    require_once('../connection.php');

    $query "SELECT budget.Budget FROM budget 
    LEFT JOIN budget2011data ON budget.Budget = budget2011data.Budget
    WHERE budget2011data.Budget IS NULL"
    ;

    if (
    $r mysql_query ($query)) { // Run the query.

    while ($row mysql_fetch_array ($r)) {
    $Budget $row['Budget'];

    echo 
    "Different " $Budget "<br>";
        } 
    }
    ?> 
    Some HTML
    Source: http://psoug.org/snippet/Opposite_of_UNION_4.htm

Similar Threads

  1. PHP mysql query then output
    By skullbox in forum Programming Discussion
    Replies: 4
    Last Post: 03-26-2011, 11:03 AM
  2. MySQL Select Query - Selecing 2 tables with 1 query
    By SmashedPumpkins in forum Programming Discussion
    Replies: 2
    Last Post: 04-22-2010, 01:45 PM
  3. PHP/MySQL Query, replace output?
    By RW-Steven in forum Programming Discussion
    Replies: 2
    Last Post: 04-28-2009, 02:44 AM
  4. creating multiple tables (MySQL) in one query
    By smidwap in forum Programming Discussion
    Replies: 5
    Last Post: 08-30-2004, 03:41 AM
  5. php/mysql question: multiple tables query
    By dundup in forum Programming Discussion
    Replies: 5
    Last Post: 05-07-2004, 05:29 PM

Posting Permissions

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