[英]php compare two sql tables and look for differences
我正在使用兩個表:
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
包含最新的值)=>打印給當前用戶。
我正在使用以下查詢(對於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
與參考數據庫criter_live
有所不同(在worksheets
應用更改)
檢查criter_live
和worksheets
: entry_date
一定entry_number
是在不同的worksheets
從裁判分貝criter_live
(施加在工作表上的變化)
檢查criter_live
和worksheets
:一個新的entry_number
出現在criter_live
它不會出現在worksheets
:在創建新行worksheets
。
檢查criter_live
和worksheets
:一個entry_number
將不再出現在criter_live
但存在worksheets
(在刪除記錄的worksheets
)
謝謝
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_live
,另一個記錄在worksheets
, entry_number
匹配但left_number
不匹配,該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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.