我有两个 SQL 表,一个包含“手动插入的数据”,另一个包含“通过脚本自动插入的数据”。
为了测试脚本是否正常运行,手动表和自动表是一样的。
所以,我想“比较”两个数据库,然后在另一个脚本中突出显示差异。
// $currentdate_today_midnight is a timestamp
$sql_getLive = "SELECT * FROM worksheets WHERE entry_date > $currentdate_today_midnight";
$req_getLive = $cnx->query($sql_getLive);
$req_getLive->setFetchMode(PDO::FETCH_OBJ);
// countAll counts all rows for a table and a condition
$countLive = countAll("worksheets", "entry_date > $currentdate_today_midnight");
$sql_getCriter = "SELECT * FROM criter_live WHERE entry_date > $currentdate_today_midnight";
$req_getCriter = $cnx->query($sql_getCriter);
$req_getCriter->setFetchMode(PDO::FETCH_OBJ);
$countCriter = countAll("criter_live", "entry_date > $currentdate_today_midnight");
if($countLive == 0){
/* If there is no live (manual datas) inserted */
echo "EMPTY";
die();
}
while ($check_criter = $req_getCriter->fetch()) {
while ($check_live = $req_getLive->fetch()) {
if ($check_live->train_id == $check_criter->train_id) {
/* check_live = worksheets */
/* check_criter = criter_live */
echo $check_live->train_id . "|" . $check_criter->entry_date . "|" . $check_live->entry_date . "|". $check_criter->left_date . "|". $check_live->entry_date . "|". $check_criter->train_type . "|". $check_live->train_type . "|". $check_criter->entry_number . "|". $check_live->entry_number . "|". $check_criter->left_number . "|". $check_live->left_number. "#";
}
}
}
因此,我尝试制作“一会儿一会儿”,但它不起作用,我只得到一个“echo ”……而不是 17 个(由于 countAll 函数返回)。
我做错了吗?或者还有其他解决办法吗?
谢谢!
最佳答案
也许你可以尝试像这样直接在 sql 中找到差异:
select * from `worksheets` where `entry_date` > $currentdate_today_midnight
and `train_id` not in (
select `train_id` from `criter_live` where `entry_date` > $currentdate_today_midnight
)
稍作修改的版本,测试entry_date
select * from `worksheets` where `entry_date` > $currentdate_today_midnight
and `entry_date` not in (
select `entry_date` from `criter_live` where `entry_date` > $currentdate_today_midnight
)
关于php - While in while 只显示一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35172312/