我的MySQL中有两个表,它们在列方面完全相同,我们可以将它们称为id、name、path、date。现在,我想对这两个表进行比较,以便得到表1与表2的区别,以及表2与表1的不同之处。我只想在我的Java程序中打印它,现在,一个简单的差异的System.out.print将是完美的。
我只想检查两个表中name列中条目的差异。任何帮助都是非常感谢的!
发布于 2017-03-21 06:05:48
如果您只想看到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中不匹配的名称。
发布于 2017-03-20 20:39:21
这将连接id上的两个表,并显示名称不同的行:
select a.name, b.name
from table1 a
join table2 b on a.id = b.id
where a.name <> b.name发布于 2017-03-23 05:25:05
reaanb答复是正确的,应该被接受。
下面是Java代码示例,它显示了他的SQL代码。我不使用MySQL (如问题中所示)。相反,本例使用H2数据库引擎,创建一个临时内存数据库。
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: Beerhttps://stackoverflow.com/questions/42913478
复制相似问题