我有2个数据库,DB1中只有1个表,DB2中有2个表 . DB1.table1中的每条记录都被拆分并分别存储在DB1.table1和DB @ .table2中 .
For example, DB1 has a table1 which looks like
Student_Name Id Address Attendance Marks
------------ -- ------- ---------- -----
John 1 90th st 70 90
The records that are transferred from DB1.table1 are stored in DB2.table and DB2.table2 in the following manner
DB2.table 1: Id Student_Name Address
-- ------------ -------
1 John 90th st
DB2.table 2: Id Attendance Marks
-- ---------- -----
1 70 90
我想编写一个测试用例,以确保将DB1中的所有数据都复制到DB2 . 我写了一些查询来确定DB1中的记录是否未复制到DB2 . 除了找出丢失的记录之外,我还想逐列检查每条记录,以确保DB1和DB2中的值相同 .
从上面的例子中,我想检查ID = 1,如果DB2.table1 Student_name = DB1.table1 Student_name,DB2.table1 Address = DB1.table1 Address,依此类推..
如果我有1000列怎么办?我应该写一个长脚本来检查每一列吗?不,这是进行此类测试的最佳方式吗?有没有可以使用的工具或者我应该写下脚本?
3 回答
这将从
db1.table1
中找到db2.table1
和db2.table2
中没有匹配项的任何Id
行 .它假定两个表中的列名称相同,并且
db2.table1
或db2.table2
中存在的任何列应在db1.table1
中具有匹配的列名称 . 因此,如果db2.table2
具有名为Foo
的列,则db1.table1
也必须具有名为Foo
的列 . 如果db2.table1
具有名为Bar
的列,则db1.table1
也必须具有名为Bar
的列 . 如果该列存在于db2
但不存在于db1
中,则会出现MySQL错误 .希望这是你想要的!
您可以使用union all和group by:
此查询的结果将为您提供DB2中与DB1不同的行数,如果行不存在但它也应该计数 . 如果结果为0,一切正常 .