Php 测试两个mysql数据库之间的内容
我有两个数据库,在DB1中只有一个表,在DB2中只有两个表。DB1.table1中的每条记录都被拆分并存储在DB1.table1和DB@.table2分别Php 测试两个mysql数据库之间的内容,php,mysql,testing,Php,Mysql,Testing,我有两个数据库,在DB1中只有一个表,在DB2中只有两个表。DB1.table1中的每条记录都被拆分并存储在DB1.table1和DB@.table2分别 For example, DB1 has a table1 which looks like Student_Name Id Address Attendance Marks ------------ -- ------- ---------- ----- Jo
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列呢?我应该写一个长脚本来检查每一列吗?不,做这种测试最好的方法是什么?有什么工具我可以使用,或者我应该写下脚本吗 这将从
db1.table1
中找到任何Id
s行,这些行在db2.table1
和db2.table2
中没有匹配项
它假定两个表中的列名称相同,并且db2.table1
或db2.table2
中存在的任何列都应该在db1.table1
中具有匹配的列名。因此,如果db2.table2
有一列名为Foo
,db1.table1
也必须有一列名为Foo
。如果db2.table1
有一列名为Bar
,db1.table1
也必须有一列名为Bar
。如果该列存在于db2
中,但不存在于db1
中,则会出现MySQL错误
希望这就是你想要的
header("Content-type: text/plain");
// connect with mysqli
// get a list of columns in db2.table1 and db2.table2
$columns = array();
$query = mysqli_query("SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = 'db2' AND table_name IN ('table1', 'table2')");
while ($row = $mysqli_fetch_assoc($query)) {
$columns[$row["table_name"]][] = "db1.table1.{$row["column_name"]} = db2.{$row["table_name"]}.{$row["column_name"]}";
}
$query = mysqli_query("
SELECT db1.table1.Id
FROM
db1.table1
LEFT JOIN db2.table1
ON ". implode(" AND ", $columns["table1"]) ."
LEFT JOIN db2.table2
ON ". implode(" AND ", $columns["table2"]) ."
WHERE
db2.table1.Id IS NULL
OR db2.table2.Id IS NULL
");
while (list($id) = mysqli_fetch_row($query)) {
echo "ID {$id} does not match\n";
}
您可以通过以下方式使用union all和group来执行此操作:
select Student_Name, Id, Address, Attendance, Marks,
(case when max(which) = 'db1' then 'Missing in db2'
when min(which) = 'db2' then 'Missing in db1'
end) as why
from ((select 'db1' as which, Student_Name, Id, Address, Attendance, Marks
from db1.table1 t
) union all
(select 'db2' as which, t1.Student_Name, t1.id, t1.Address, t2.Attendance, t2.Marks
from db2.table1 t1 join
db2.table2 t2
on t1.id = t2.id
)
) u
group by Student_Name, Id, Address, Attendance, Marks
having count(distinct which) = 1
这个查询的结果将给出DB2中不同于DB1的行数,若行不存在但应该存在,它也会计算。如果结果为0,则一切正常
SELECT
COUNT(*) AS difference
FROM
DB1.table1 AS d1t1
LEFT JOIN DB2.table1 AS d2t1
USING (Id)
LEFT JOIN DB2.table2 AS d2t2
USING (Id)
WHERE
(d1t1.Student_Name <> d2t1.Student_Name)
OR (d1t1.Address <> d2t1.Address)
OR (d1t1.Attendace <> d2t2.Attendace)
OR (d1t1.Marks <> d2t2.Marks)
选择
将(*)计为差额
从…起
DB1.1表1为d1t1
左键将DB2.table1作为d2t1连接
使用(Id)
左键将DB2.table2作为d2t2连接
使用(Id)
哪里
(d1t1.学生姓名d2t1.学生姓名)
或(d1t1.地址d2t1.地址)
或(d1t1.Attendace d2t2.Attendace)
或(d1t1.Marks d2t2.Marks)
您正在寻找数据库复制?否。我想测试每个值是否都被正确复制。在尝试脚本时,我遇到了一个常见错误警告:mysql_fetch_row()希望参数1是资源,布尔值在..
中给出,但可以解决它。我看到$query已经执行,但仍然是错误。在mysqli\u query
两行中,在分号前面添加此项:或die(mysqli\u error())
。这将告诉您它得到的错误。我无法真正测试第一个获取列的查询,因此我无法真正测试第二个包含内爆内容的查询。获得错误应该会为您指明正确的方向。