如何比较两个表中的数据

时间:2017-03-13 11:40:51

标签: php mysql

我有两个具有相同结构的数据库(mysql)。我想:

  • 比较两个表中的数据。表一 - 家庭和第二部工作,
  • 发送包含结果的电子邮件,
  • 更新表格工作中的数据。

我的查询:

select id, code, quantity from wpx_products

我在表home和work(两个数据库)中运行此查询。这是输出:

"3";"home 005-07";"2"
"63";"home 033-12";"2"
"15";"home 005-19";"2"

和工作:

"1";"work 005-07";"2"
"2";"work 033-12";"5"
"3";"work 005-19";"2"

我想做什么?我的意思是"比较" ?我希望找到记录,在列'代码'中排除标记工作或主页。例如,我想找到033-12并检查数量。如果差异复制值从家到工作。

在第一秒我想在mysql中使用trigger。但这对我来说不是解决方案,因为我不能自己运行它而且我无法发送带有结果的电子邮件。实现此功能的最佳方法是什么?谢谢你的帮助。

亲切的问候

---------------------编辑------------------------- ---

我在下面查看此代码(感谢#AntG)。我有一个问题。当我打印

foreach ($result_target AS $target) {
    $code_target = substr($target['code'], 4);
    if ($code_source === $code_target) {
        if ($source['quantity'] !== $target['quantity']) {
            print $source['quantity'] .' -> ' . $target['quantity']."<br /><br />";
            $match[] = array('code' => $source['code'], 'quantity' => $source['quantity'], 'targetid' => $target['id'], 'sourceid' => $source['id']);      
        }
        $found = true;
        break;
    }
}

我有这样的结果:documentation of the library就像你看到有50个值。当我打印$ match时,会有更多,重复的值,我不知道为什么?

$msg = '';
foreach ($match AS $entry) {
    $msg .= 'Change identified: Home_ID=' . $entry['sourceid'] . ' code: ' . $entry['code'] . ' quantity:' . $entry['quantity'] . PHP_EOL . '<br />';
    print $msg;
    /*  Perform DB updates using $entry['targetid'] and $entry['quantity'] */
}

我有这样的结果:http://suszek.info/projekt1/所有代码:

 $match = array(); $new = array();

 foreach ($result_source AS $source) {

     $found = false;
     $code_source = substr($source['code'], 4);
     foreach ($result_target AS $target) {
         $code_target = substr($target['code'], 4);
         if ($code_source === $code_target) {
             if ($source['quantity'] !== $target['quantity']) {
                 print $source['quantity'] .' -> ' . $target['quantity']."<br /><br />";
                 $match[] = array('code' => $source['code'], 'quantity' => $source['quantity'], 'targetid' => $target['id'], 'sourceid' => $source['id']);      
             }
             $found = true;
             break;
         }
     }

     if (!$found) {
         $new[] = array('code' => $source['code'], 'quantity' => $source['quantity'], 'sourceid' => $source['id']);
     } } $msg = ''; foreach ($match AS $entry) {
     $msg .= 'Change identified: Home_ID=' . $entry['sourceid'] . ' code: ' . $entry['code'] . ' quantity:' . $entry['quantity'] . PHP_EOL
 . '<br />';
     print $msg;
     /*  Perform DB updates using $entry['targetid'] and $entry['quantity'] */ }

 foreach ($new AS $entry) {
     $msg .= 'New Entry: Home_ID=' . $entry['sourceid'] . ' code: ' . $entry['code'] . ' quantity:' . $entry['quantity'] . PHP_EOL . '<br
 />';
     #print $msg;
     /*  Perform DB inserts using $entry['code'] and $entry['quantity'] if this is desired behaviour */ }

 /*  Send email with $msg  */

1 个答案:

答案 0 :(得分:0)

如果你在$results数组中捕获两个查询结果,那么我认为substr()是你在此之后的PHP函数:

$match=array();
$new=array();
foreach($results_home AS $home)
{
    $found=false;
    $code=substr($home['code'],5);
    foreach($results_work AS $work)
    {
        if($code===substr($work,5))
        {
             if($home['quantity']!==$work['quantity'])
             {
                 $match[]=array('code'=>$home['code'],'quantity'=>$home['quantity'],'workid'=>$work['id'],'homeid'=>$home['id']);
             }
             $found=true;
             break;
        }
    }
    if(!$found)
    {
        $new[]=array('code'=>$home['code'],'quantity'=>$home['quantity'],'homeid'=>$home['id']);

    }
}
$msg='';
foreach($match AS $entry)
{
    $msg.='Change identified: Home_ID='.$entry['homeid'].' code: '.$entry['code'].' quantity:'.$entry['quantity'].PHP_EOL;
    /*  Perform DB updates using $entry['workid'] and $entry['quantity'] */
}

foreach($new AS $entry)
{
    $msg.='New Entry: Home_ID='.$entry['homeid'].' code: '.$entry['code'].' quantity:'.$entry['quantity'].PHP_EOL;
    /*  Perform DB inserts using $entry['code'] and $entry['quantity'] if this is desired behaviour */
}

/*  Send email with $msg  */