我正在处理两个表:
criter_live
通过AJAX自动更新worksheets
由用户手动更新
所以,我的问题是,我想知道自动更新表 (criter_live
) 和手动更新表 (worksheets
) 之间的区别。两个表中的 entry_number 和 left_number 相同,但是 machine_id
、entry_date
和 left_date
可能不同,所以我想查询知道什么时候worksheets
与 criter_live
不同。虽然,一行不能在 criter_live
中,但可以在 worksheets
中,反之亦然。在这种情况下,我们将创建一条新记录,或者我们将从数据库中删除一条记录。
例如,我正在检查 criter_live
和 worksheets
是否有 entry_number
ABC,但是 worksheets
没有包含最新的 left_date
值(criter_live
包含最新的值)=> 打印 smth 给我当前的用户。
我正在使用这个查询(对于 machine_id,对于 left_date,对于 entry_date):
SELECT train_id FROM criter_live WHERE entry_date > $currentdate_today_midnight AND mac_id NOT IN (SELECT train_id FROM worksheets)
但它并没有像我想要的那样工作...在某些情况下它不会像我想要的那样返回结果,我认为这是一个问题但是在哪里......事实上,我可以有几个 machine_id
同一天,但没有相同的 entry_number
或 left_number
...我应该提到在两个表中的字段 entry_number
& left_number
包含相同的值(除了缺失的行,显然不在其中一个基中......)。
具体情况下,如果不明白:
- 检查 criter_live
和 worksheets
:某个特定的 left_date
entry_number
在 worksheets
中与 ref db criter_live
不同(应用
工作表上的更改)
检查
criter_live
和worksheets
:某个特定的entry_date
entry_number
在worksheets
中与 ref dbcriter_live
不同(应用 工作表上的更改)检查
criter_live
和worksheets
:一个新的entry_number
出现在criter_live
未出现在worksheets
中:在worksheets
中创建新行。正在检查
criter_live
和worksheets
:entry_number
否 longer出现在criter_live
中,但存在于worksheets
中(删除worksheets
中的记录)
谢谢
数据库方案:
+--------------------------------------------------------------------------------------+
| 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_live
中,另一条在 worksheets
中,会发生什么情况,对于哪个 entry_number
匹配但 left_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
关于php比较两个sql表并寻找差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430488/