简体   繁体   中英

How to compare data from two tables

I have two database (mysql) with the same structure. I want to:

  • compare data in two table. Table one - home and the second work,
  • send email with results,
  • update data in table work.

My query:

select id, code, quantity from wpx_products

I run this query in table home and work (two databases). And this is output:

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

and from work:

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

What I want to do ? What I mean by "compare" ? I want find record with excluding tag work or home in column 'code'. For example I want to find 033-12 and check quantity. If the difference copy value from home to work.

In first second I want to use trigger in mysql. But this is not solution for me because I cant run it by myself and I can't send email with results. What is the best way to achieve this functionality ? Thanks for help.

Kind regards

---------------------edit----------------------------

I check this code below (thanks #AntG). And I have one problem. When I print

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;
    }
}

I have this results: http://suszek.info/projekt1/ Like You see there is 50 values. When I print $match there is much more, duplicated value and I don't know why ?

$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'] */
}

I have this results: http://suszek.info/projekt1/index_1.php And all code:

 $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  */

If you are capturing both query results in a $results array, then I think substr() is the PHP function that you are after here:

$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  */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM