compare differences between two tables in mysql

后端 未结 6 1495
北荒
北荒 2020-11-30 17:07

Same as oracle diff: how to compare two tables? except in mysql.

Suppose I have two tables, t1 and t2 which are identical in layout but which may contain

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 17:42

    Based on Haim's answer I created a PHP code to test and display all the differences between two databases. This will also display if a table is present in source or test databases. You have to change with your details the <> variables content.

    ";
        $Pass = "";
        $SourceDB = "";
        $TestDB = "";
    
        $link = new mysqli( "p:". "localhost", $User, $Pass, "" );
    
        if ( mysqli_connect_error() ) {
    
            die('Connect Error ('. mysqli_connect_errno() .') '. mysqli_connect_error());
    
        }
    
        mysqli_set_charset( $link, "utf8" );
        mb_language( "uni" );
        mb_internal_encoding( "UTF-8" );
    
        $sQuery = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="'. $SourceDB .'";';
    
        $SourceDB_Content = query( $link, $sQuery );
    
        if ( !is_array( $SourceDB_Content) ) {
    
            echo "Table $SourceDB cannot be accessed";
            exit(0);
    
        }
    
        $sQuery = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="'. $TestDB .'";';
    
        $TestDB_Content = query( $link, $sQuery );
    
        if ( !is_array( $TestDB_Content) ) {
    
            echo "Table $TestDB cannot be accessed";
            exit(0);
    
        }
    
        $SourceDB_Tables = array();
        foreach( $SourceDB_Content as $item ) {
            $SourceDB_Tables[] = $item["TABLE_NAME"];
        }
    
        $TestDB_Tables = array();
        foreach( $TestDB_Content as $item ) {
            $TestDB_Tables[] = $item["TABLE_NAME"];
        }
        //var_dump( $SourceDB_Tables, $TestDB_Tables );
        $LookupTables = array_merge( $SourceDB_Tables, $TestDB_Tables );
        $NoOfDiscrepancies = 0;
        echo "
    
        
    
        ";
    
        foreach( $LookupTables as $table ) {
    
            $FoundInSourceDB = in_array( $table, $SourceDB_Tables ) ? 1 : 0;
            $FoundInTestDB = in_array( $table, $TestDB_Tables ) ? 1 : 0;
            echo "
    
           
            ";
    
        }
    
        echo "
    
        
    Table Found in $SourceDB (". count( $SourceDB_Tables ) .") Found in $TestDB (". count( $TestDB_Tables ) .") Test result
    $table ". compareTables( $SourceDB, $TestDB, $table ) ."


    No of discrepancies found: $NoOfDiscrepancies "; function query( $link, $q ) { $result = mysqli_query( $link, $q ); $errors = mysqli_error($link); if ( $errors > "" ) { echo $errors; exit(0); } if( $result == false ) return false; else if ( $result === true ) return true; else { $rset = array(); while ( $row = mysqli_fetch_assoc( $result ) ) { $rset[] = $row; } return $rset; } } function compareTables( $source, $test, $table ) { global $link; global $NoOfDiscrepancies; $sQuery = " SELECT column_name,ordinal_position,data_type,column_type FROM ( SELECT column_name,ordinal_position, data_type,column_type,COUNT(1) rowcount FROM information_schema.columns WHERE ( (table_schema='$source' AND table_name='$table') OR (table_schema='$test' AND table_name='$table') ) AND table_name IN ('$table') GROUP BY column_name,ordinal_position, data_type,column_type HAVING COUNT(1)=1 ) A; "; $result = query( $link, $sQuery ); $data = ""; if( is_array( $result ) && count( $result ) > 0 ) { $NoOfDiscrepancies++; $data = ""; foreach( $result as $item ) { $data .= ""; } $data .= "
    column_nameordinal_positiondata_typecolumn_type
    ". $item["column_name"] ."". $item["ordinal_position"] ."". $item["data_type"] ."". $item["column_type"] ."
    "; return $data; } else { return "Checked but no discrepancies found!"; } } ?>

提交回复
热议问题