php比较两个sql表并查找差异
我正在使用两张桌子:php比较两个sql表并查找差异,php,mysql,database,Php,Mysql,Database,我正在使用两张桌子: criter\u live通过AJAX自动更新 工作表,由用户手动更新 所以,我的问题是,我想知道自动更新的表格(criter\u live)和手动更新的表格(工作表)之间的区别。条目号和左条目号在两个表中都是相同的,但是machine\u id,entry\u date和left\u date可能不同,因此我想通过查询了解criter\u live中的工作表上何时存在差异。尽管如此,一行不能在criter\u live中,而是在工作表中,反之亦然。在这种情况下,我们将
通过AJAX自动更新criter\u live
,由用户手动更新工作表
criter\u live
)和手动更新的表格(工作表
)之间的区别。条目号和左条目号在两个表中都是相同的,但是machine\u id
,entry\u date
和left\u date
可能不同,因此我想通过查询了解criter\u live
中的工作表上何时存在差异。尽管如此,一行不能在criter\u live
中,而是在工作表中,反之亦然。在这种情况下,我们将创建一个新记录,或者从数据库中删除一个记录
例如,我正在检查criter\u live
和worksheets
上的entry\u number
ABC,但是worksheets
不包含最新的left\u date
值(criter\u live
包含最新值)=>将smth打印给当前用户
我使用了这个查询(对于机器id,对于左日期,对于输入日期):
但它不能像我想的那样工作。。。在某些情况下,它不会返回我想要的结果,我认为这是一个问题,但在哪里。。。事实上,我可以在同一天拥有多个机器id
,但是没有相同的条目号
或左侧编号
。。。我应该提到的是,在这两个表中,entry\u number
&left\u number
字段包含相同的值(除了缺少一行,该行显然不在其中一个基中…)
在具体情况下,如果您不了解:
-检查criter\u live
和工作表
:left\u日期
,查看特定时间
条目号
在工作表中与ref dbcriter\u live
不同(应用
工作表上的更改)
- 检查
criter\u live
和工作表
:entry\u date
,查看特定时间
条目号
在工作表中与ref dbcriter\u live
不同(应用
工作表上的更改)
- 检查
criter\u live
和工作表
:新的条目号
出现在
criter\u live
未出现在工作表中:在工作表中创建新行
- 检查
criter\u live
和工作表
:一个条目号
否
不再出现在criter\u live
中,但存在于工作表中(删除工作表中的记录)
多谢各位
Db方案:
+--------------------------------------------------------------------------------------+
| criter_live & worksheets |
+--------------------------------------------------------------------------------------+
| id | machine_id | entry_number | machine_type | entry_date | left_date | left_number |
+----+------------+--------------+--------------+------------+-----------+-------------+
| 1 | 76801 | R88901 | -Z | timestamp | timestamp | S79980 |
+----+------------+--------------+--------------+------------+-----------+-------------+
| 2 | 82501 | R89874 | -X | timestamp | timestamp | S98780 |
+--------------------------------------------------------------------------------------+有很多问题没有解决,例如,如果有两个记录,一个在criter\u live
中,另一个在工作表中,会发生什么,哪个entry\u number
匹配,但left\u number
不匹配?无论如何,我觉得解决这个问题的方法不是在一个过于复杂的SQL查询中,而是在一个简单的PHP脚本中(我假设使用PHP是可以的,因为您的问题中包含了PHP
标记)
因此,下面是一个关于PHP如何运行的伪代码。不是为了方便地运行PHP,只是一个框架:
function records_match (record_from_criter_live, record_from_worksheets)
{
// The code below this function assumes the following two lines
if (record_from_criter_live['entry_number'] != record_from_worksheets['entry_number'])
return false;
// That said, you can implement here any further criteria
// you wish to add to decide if two records
// (one record from table criter_live and the other from worksheets)
// are "the same" or not
// Return true if they match
// Return false if they don't
}
// The following two lines are intended to be
// PHP code for establishing the connection to the database
// and setting up everything.
// Note that it is assumed that ORDER BY entry_number
// will always give all matching record pairs
// in the correct order.
// That's why the function above must always return false
// for any two records with different entry_number
query1 = SELECT * FROM criter_live ORDER BY entry_number
query2 = SELECT * FROM worksheets ORDER BY entry_number
// Again, these two lines represent
// PHP code for getting a record from each query
// It is assumed that record1 and record2 get a special value
// when trying to get a record past the last one
// The condition in the while loop checks for this
record1 = first record from query1
record2 = first record from query2
while ((record1 not past end-of-table) OR (record2 not past end-of-table))
{
// variable next means:
// $next = 1 -> record1 is orphan, no matching record2 exists
// $next = 2 -> record2 is orphan, no matching record1 exists
// $next = 3 -> record1 and record2 match
if (record1 is past end-of-table)
$next = 2
else if (record2 is past end-of-table)
$next = 1
else if (records_match (record1, record2)) // Notice this is a call to function above
$next = 3
else if (record1[entry_number] < record2[entry_number])
$next = 1;
else
$next = 2;
// Now process the result
if ($next == 1)
{
// Add record1 to list of criter_live
// with no matching worksheets
do_whatever_with (record1)
// Then move forward
record1 = next record from query1
}
else if ($next == 2)
{
// Add record2 to list of worksheets
// with no matching criter_live
do_whatever_with (record2)
// Then move forward
record2 = next record from query2
}
else
{
// Add (record1, record2) to list of matching (criter_live, worksheets)
// I suppose this means just ignore both record1 and record2
// i.e. I suppose the following line is innecesary
do_whatever_with (record1, record2)
// Then move forward
record1 = next record from query1
record2 = next record from query2
}
}
close query1 and query2 as necessary
function records\u match(从criter\u live记录,从工作表记录)
{
//此函数下面的代码采用以下两行
如果(从社区生活记录['entry\u number']!=从工作表记录['entry\u number'])
返回false;
//也就是说,您可以在这里实现任何进一步的标准
//您希望添加以决定是否有两条记录
//(一条记录来自criter_live表格,另一条记录来自工作表)
//“相同”与否
//如果匹配,则返回true
//如果没有,则返回false
}
//以下两行旨在:
//用于建立到数据库的连接的PHP代码
//以及安排一切。
//请注意,假定按条目编号排序
//将始终提供所有匹配的记录对
//按正确的顺序。
//这就是为什么上面的函数必须总是返回false
//对于具有不同条目号的任意两个记录
query1=根据条目编号从criter\U live ORDER中选择*
query2=按条目编号从工作表中选择*
//同样,这两条线代表
//用于从每个查询中获取记录的PHP代码
//假设record1和record2获得一个特殊值
//当试图让一张唱片超过最后一张时
//while循环中的条件检查是否存在此问题
record1=查询1中的第一条记录
record2=查询2中的第一条记录
而((记录1未超过表尾)或(记录2未超过表尾))
{
//变量next表示:
//$next=1->记录1为孤立记录,不存在匹配的记录2
//$next=2->record2为孤立记录,不存在匹配的record1
//$next=3->记录1和记录2匹配
如果(记录1超过表的末尾)
$next=2
else if(记录2超过表的末尾)
$next=1
else if(records\u match(record1,record2))//注意这是对上面函数的调用
$next=3
else if(记录1[条目编号]<记录2[条目编号])
$next=1;
其他的
$next=2;
//现在处理结果
如果($next==1)
{
//将record1添加到criter_live列表中
//没有匹配的工作表
做任何事(记录1)
//然后继续前进
record1=查询1中的下一条记录
}
else if($next==2)
{
//将记录2添加到工作表列表中
//没有匹配的生物
做任何事(记录2)
//然后继续前进
record2=查询2中的下一条记录
}
其他的
{
//添加(记录)
function records_match (record_from_criter_live, record_from_worksheets)
{
// The code below this function assumes the following two lines
if (record_from_criter_live['entry_number'] != record_from_worksheets['entry_number'])
return false;
// That said, you can implement here any further criteria
// you wish to add to decide if two records
// (one record from table criter_live and the other from worksheets)
// are "the same" or not
// Return true if they match
// Return false if they don't
}
// The following two lines are intended to be
// PHP code for establishing the connection to the database
// and setting up everything.
// Note that it is assumed that ORDER BY entry_number
// will always give all matching record pairs
// in the correct order.
// That's why the function above must always return false
// for any two records with different entry_number
query1 = SELECT * FROM criter_live ORDER BY entry_number
query2 = SELECT * FROM worksheets ORDER BY entry_number
// Again, these two lines represent
// PHP code for getting a record from each query
// It is assumed that record1 and record2 get a special value
// when trying to get a record past the last one
// The condition in the while loop checks for this
record1 = first record from query1
record2 = first record from query2
while ((record1 not past end-of-table) OR (record2 not past end-of-table))
{
// variable next means:
// $next = 1 -> record1 is orphan, no matching record2 exists
// $next = 2 -> record2 is orphan, no matching record1 exists
// $next = 3 -> record1 and record2 match
if (record1 is past end-of-table)
$next = 2
else if (record2 is past end-of-table)
$next = 1
else if (records_match (record1, record2)) // Notice this is a call to function above
$next = 3
else if (record1[entry_number] < record2[entry_number])
$next = 1;
else
$next = 2;
// Now process the result
if ($next == 1)
{
// Add record1 to list of criter_live
// with no matching worksheets
do_whatever_with (record1)
// Then move forward
record1 = next record from query1
}
else if ($next == 2)
{
// Add record2 to list of worksheets
// with no matching criter_live
do_whatever_with (record2)
// Then move forward
record2 = next record from query2
}
else
{
// Add (record1, record2) to list of matching (criter_live, worksheets)
// I suppose this means just ignore both record1 and record2
// i.e. I suppose the following line is innecesary
do_whatever_with (record1, record2)
// Then move forward
record1 = next record from query1
record2 = next record from query2
}
}
close query1 and query2 as necessary