在一段时间内只显示一行


While in a while display only one row?

我有两个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. "#";
        }
    }
}
所以,我

试图"一段时间内",但它不起作用,我只得到一个"回声"......而不是 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
    )

编辑:我假设您在两个表中都有相同的数据。

删除第二个while循环:

while ($check_criter = $req_getCriter->fetch()) {
    $check_live = $req_getLive->fetch();
    if ($check_live->train_id == $check_criter->train_id) {
        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. "#";
    }
}

基本上,在外部循环的第一次迭代中,您一直在从$req_getCriter中获取 1 行并将其与 $req_getLive 中的所有其他行进行比较。第二次迭代不起作用,因为提取了$req_getLive中的所有行。

在外部循环的第一次迭代之后,您将从数据库结果集中获取内部循环中的所有项,因此它永远不会再次运行。

当然,您可以从数组中的两个查询中获取所有项目并使用foreach循环重置内部循环的数据库游标,但您也可以在一个数据库查询中执行此操作。

如果两个表中train_id序列相同,akasummer的答案将正常工作。如果序列不同,则可能会遗漏某些行。

如果两个表中的train_id顺序没有差异,则不需要在赤夏的答案中以条件为条件。

if ($check_live->train_id == $check_criter->train_id) 
最简单的

方法是我在 mysql 中的内部连接,根据train_id和那里的输入日期从两个表中获取数据。如下图所示(语法上可能会有一些愚蠢的错误,但逻辑是可以理解的)

SELECT 
   W.*, 
   CL.entry_date AS cl_entry_date, 
   CL.left_date AS cl_left_date, 
   CL.train_type AS cl_train_type, 
   CL.entry_number AS cl_entry_number, 
   CL.left_number AS cl_left_number  
FROM 
   worksheets AS W, 
   criter_live AS CL 
WHERE 
   W.train_id = CL.train_id 
AND 
  W.entry_date > $currentdate_today_midnight 
AND 
  CL.entry_date > $currentdate_today_midnight

在一个结果中,您将获得两个表的列,然后您可以使用简单的普通 while 循环进行检查。