Php 如何比较两个表中的数据
我有两个结构相同的数据库(mysql)。我想:Php 如何比较两个表中的数据,php,mysql,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";
- 比较两个表中的数据。表一——家庭和第二项工作
- 发送包含结果的电子邮件
- 更新表格工作中的数据
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中使用触发器。但这不是我的解决方案,因为我不能自己运行它,也不能发送带有结果的电子邮件。实现此功能的最佳方式是什么?谢谢你的帮助
问候
---------------------编辑----------------------------
我检查下面的代码(谢谢#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;
}
}
foreach($result\u target作为$target){
$code_target=substr($target['code'],4);
如果($code\u source===$code\u target){
如果($source['quantity']!=$target['quantity']){
打印$source['quantity'..->.$target['quantity'.]。“
”;
$match[]=array('code'=>$source['code'],'quantity'=>$source['quantity'],'targetid'=>$target['id'],'sourceid'=>$source['id']);
}
$found=true;
打破
}
}
我有这样的结果:就像你看到的,有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'] */
}
$msg='';
foreach($match AS$entry){
$msg.='Change identified:Home_ID='.$entry['sourceid']。'code:'.$entry['code']。'quantity:'.$entry['quantity'].PHP_EOL.
;
打印$msg;
/*使用$entry['targetid']和$entry['quantity']执行数据库更新*/
}
我有以下结果:和所有代码:
$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 */
$match=array()$new=array();
foreach($result\u source作为$source){
$found=false;
$code_source=substr($source['code'],4);
foreach($result\u target作为$target){
$code_target=substr($target['code'],4);
如果($code\u source===$code\u target){
如果($source['quantity']!=$target['quantity']){
打印$source['quantity'..->.$target['quantity'.]。“
”;
$match[]=array('code'=>$source['code'],'quantity'=>$source['quantity'],'targetid'=>$target['id'],'sourceid'=>$source['id']);
}
$found=true;
打破
}
}
如果(!$found){
$new[]=array('code'=>$source['code'],'quantity'=>$source['quantity'],'sourceid'=>$source['id']);
}}$msg='';foreach($match AS$entry){
$msg.='Change identified:Home\u ID='.$entry['sourceid'.'代码:'.$entry['code'.'数量:'.$entry['quantity'.]PHP\u EOL
“
”;
打印$msg;
/*使用$entry['targetid']和$entry['quantity']*/}执行数据库更新
foreach($new AS$entry){
$msg.='New Entry:Home_ID='.$Entry['sourceid'.'代码:'.$Entry['code'.'数量:'.$Entry['quantity'.]PHP_EOL.
';
#打印$msg;
/*如果需要,使用$entry['code']和$entry['quantity']执行数据库插入*/}
/*发送带有$msg的电子邮件*/
如果您在$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 */
你能发布这两个表结构吗?两个表之间的关系定义了吗?理论上,我只能给出一个例子来反映问题的规模。这两个表之间没有关系。我认为最好使用数组。为什么不将
mysqli\u query
的$result
放入一个数组中呢。例如$mHomeTableArray
和$mWorkTableArray
。你能这样做然后打印两个数组吗?然后呢?你知道吗?你的链接给了我404个错误,所以我看不到。但是如果您的print语句显示了正确的结果,那么$match
数组中应该包含完全相同的内容,因此我不确定是什么原因导致了重复。请更新上面的描述。