Sven.P
Sven.P

Reputation: 33

Compare two tables with each other and get difference printout

I have two tables in my MySQL that are exactly the same in terms of columns, we can call them id, name, path, date. Now I would like to get a comparison of these two tables in order to get how table 1 differs to table 2 and how table 2 differs to table 1. I would simply like to print it in my Java program, for now a simple System.out.print of the difference would be perfect.

I only want to check the difference of entries in the name column in the both tables. Any help is super appreciated!

Upvotes: 1

Views: 7341

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 340070

The Answer by reaanb is correct and should be accepted.

Here is example Java JDBC code showing his SQL code in action. I do not use MySQL (as in the Question). Instead this example uses the H2 Database Engine, creating a temporary in-memory database.

try {
    Class.forName ( "org.h2.Driver" );
} catch ( final ClassNotFoundException e ) {
    e.printStackTrace ( );
}
try ( Connection conn = DriverManager.getConnection ( "jdbc:h2:mem:trash_me_db" ) ;
      Statement stmt = conn.createStatement ( ) ) {
    String sql = null;

    // Table 1
    sql = "CREATE TABLE t1_ (\n" +
            "  id_ INT IDENTITY ,\n" +
            "  name_ VARCHAR \n" +
            ");";
    stmt.executeUpdate ( sql );
    stmt.execute ( "INSERT INTO t1_ ( name_ ) VALUES ('Jesse');" );
    stmt.execute ( "INSERT INTO t1_ ( name_ ) VALUES ('Wendy');" );
    stmt.execute ( "INSERT INTO t1_ ( name_ ) VALUES ('Lisa');" );
    stmt.execute ( "INSERT INTO t1_ ( name_ ) VALUES ('Susan');" );
    stmt.execute ( "INSERT INTO t1_ ( name_ ) VALUES ('Jimmy');" );
    stmt.execute ( "INSERT INTO t1_ ( name_ ) VALUES ('Wine');" );

    // Dump
    System.out.println ( "\nDump table t1_" );
    try ( ResultSet rs = stmt.executeQuery ( "SELECT * FROM t1_ ;" ) ) {
        while ( rs.next ( ) ) {
            final int id = rs.getInt ( "id_" );    //Retrieve by column name
            final String name = rs.getString ( "name_" );
            System.out.println ( "id: " + id + " | name: " + name );
        }
    }

    // Table 2
    sql = "CREATE TABLE t2_ (\n" +
            "  id_ INT IDENTITY ,\n" +
            "  name_ VARCHAR \n" +
            ");";
    stmt.executeUpdate ( sql );
    stmt.execute ( "INSERT INTO t2_ ( name_ ) VALUES ('Jesse');" );
    stmt.execute ( "INSERT INTO t2_ ( name_ ) VALUES ('Wendy');" );
    stmt.execute ( "INSERT INTO t2_ ( name_ ) VALUES ('Lisa');" );
    stmt.execute ( "INSERT INTO t2_ ( name_ ) VALUES ('Susan');" );
    stmt.execute ( "INSERT INTO t2_ ( name_ ) VALUES ('Jimmy');" );
    stmt.execute ( "INSERT INTO t2_ ( name_ ) VALUES ('Beer');" );

    // Dump
    System.out.println ( "\nDump table t2_" );
    try ( ResultSet rs = stmt.executeQuery ( "SELECT * FROM t2_ ;" ) ) {
        while ( rs.next ( ) ) {
            final int id = rs.getInt ( "id_" );    //Retrieve by column name
            final String name = rs.getString ( "name_" );
            System.out.println ( "id: " + id + " | name: " + name );
        }
    }

    // Difference, left.
    System.out.println ( "\nShow names that exist in t1_ but not in t2_." );
    sql = "SELECT *\n" +
            "FROM t1_ \n" +
            "LEFT JOIN t2_ ON t1_.name_ = t2_.name_\n" +
            "WHERE t2_.name_ IS NULL";
    try ( ResultSet rs = stmt.executeQuery ( sql ) ) {
        while ( rs.next ( ) ) {
            final int id = rs.getInt ( "id_" );    //Retrieve by column name
            final String name = rs.getString ( "name_" );
            System.out.println ( "id: " + id + " | name: " + name );
        }
    }

    // Difference, right.
    System.out.println ( "\nShow names that exist in t2_ but not in t1_." );
    sql = "SELECT *\n" +
            "FROM t2_ \n" +
            "LEFT JOIN t1_ ON t2_.name_ = t1_.name_\n" +
            "WHERE t1_.name_ IS NULL";
    try ( ResultSet rs = stmt.executeQuery ( sql ) ) {
        while ( rs.next ( ) ) {
            final int id = rs.getInt ( "id_" );    //Retrieve by column name
            final String name = rs.getString ( "name_" );
            System.out.println ( "id: " + id + " | name: " + name );
        }
    }


} catch ( final SQLException e ) {
    e.printStackTrace ( );
}

When run.

Dump table t1_
id: 1 | name: Jesse
id: 2 | name: Wendy
id: 3 | name: Lisa
id: 4 | name: Susan
id: 5 | name: Jimmy
id: 6 | name: Wine

Dump table t2_
id: 1 | name: Jesse
id: 2 | name: Wendy
id: 3 | name: Lisa
id: 4 | name: Susan
id: 5 | name: Jimmy
id: 6 | name: Beer

Show names that exist in t1_ but not in t2_.
id: 6 | name: Wine

Show names that exist in t2_ but not in t1_.
id: 6 | name: Beer

Upvotes: 1

reaanb
reaanb

Reputation: 10064

If you just want to see a list of names that exist in table1 but not in table2, try the following:

SELECT a.name
FROM table1 a
LEFT JOIN table2 b ON a.name = b.name
WHERE table2.name IS NULL

Swap table1 and table2 in that query to get the unmatched names in table2.

Upvotes: 2

YouKnowWhoIAm
YouKnowWhoIAm

Reputation: 1

I know for sure that you could diff two databases and see differences between them like (test db and prod db)

here is the documentation

https://dev.mysql.com/doc/mysql-utilities/1.5/en/mysqldbcompare.html

Upvotes: 0

Forklift
Forklift

Reputation: 969

This will join 2 tables on id and show you rows where name is different:

select a.name, b.name 
from table1 a 
join table2 b on a.id = b.id
where a.name <> b.name

Upvotes: 1

Related Questions