将两个表相互比较并获得差异打印输出

时间:2017-03-20 20:36:21

标签: java mysql sql database diff

我的MySQL中有两个表在列方面完全相同,我们可以将它们称为id,name,path,date。现在我想对这两个表进行比较,以获得表1与表2的不同之处以及表2与表1的不同之处。我只想在我的Java程序中打印它,现在简单{{ 1}}差异将是完美的。

我只想检查两个表中name列中条目的区别。任何帮助都非常感激!

4 个答案:

答案 0 :(得分:2)

如果您只想查看table1中存在的名称列表,但不想查看table2中的名称列表,请尝试以下操作:

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

在该查询中交换table1和table2以获取table2中不匹配的名称。

答案 1 :(得分:1)

这将在id上连接2个表,并显示名称不同的行:

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

答案 2 :(得分:1)

Answer by reaanb是正确的,应该被接受。

以下示例Java JDBC代码显示了他的SQL代码。我不使用MySQL(如问题中所述)。相反,此示例使用H2 Database Engine,创建临时内存数据库。

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 ( );
}

跑步时。

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

答案 3 :(得分:0)

我确信您可以区分两个数据库并查看它们之间的差异,例如(test db和prod db)

这是文档

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