簡體   English   中英

php比較兩個sql表並查找差異

[英]php compare two sql tables and look for differences

我正在使用兩個表:

  • criter_live ,可通過AJAX自動更新
  • 用戶手動更新的worksheets

所以,我的問題是,我想知道自動更新的表( criter_live )和手動更新的表( worksheets )之間的區別。 兩個表中的entry_number和left_number相同,但是machine_identry_dateleft_date可以不同,因此我希望查詢知道何時worksheetscriter_live有所不同。 雖然,行不能在criter_live但可以在worksheets ,反之亦然。 在這種情況下,我們將創建一條新記錄,或者從數據庫中刪除一條記錄。

例如,我正在檢查criter_liveworksheetsentry_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_numberleft_number ...我應該提到的是,在兩個表中, entry_numberleft_number字段包含相同的值(除了缺少的行明顯不在基數之一之外...)。

在具體情況下,如果您不了解以下內容:-檢查criter_liveworksheetsleft_date中某個entry_number worksheets與參考數據庫criter_live有所不同(在worksheets應用更改)

  • 檢查criter_liveworksheetsentry_date一定entry_number是在不同的worksheets從裁判分貝criter_live (施加在工作表上的變化)

  • 檢查criter_liveworksheets :一個新的entry_number出現在criter_live它不會出現在worksheets :在創建新行worksheets

  • 檢查criter_liveworksheets :一個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 ,另一個記錄在worksheetsentry_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM