2015-10-21 60 views
0

我是新來的PHP,我想弄清楚如何比較兩個mysql查詢結果並回顯出不同之處。基本上我有一個比較狀態標誌特徵的數據庫。用戶將選擇2個狀態,我想要的是讓php回顯3組結果(僅狀態1,共享特性和狀態2)。我下面的粗略代碼只反映了狀態1和狀態2的特徵。有沒有一種方法可以比較查詢結果,以便我得到狀態之間的差異以及共享特徵。使用PHP來比較mysql結果,然後回顯差異?

<?php $query1 = $_GET['query1']; 
     $query2 = $_GET['query2']; 
     $min_length = 2;?> 

<!-- state 1 characteristics only --> 

<div class="fluid s1charc"> 

<?php echo "<strong><p>Search results for $query1</strong></p>";?> 

<?php if(strlen($query1) >= $min_length){$query1 = htmlspecialchars($query1); 
     $query1 = mysql_real_escape_string($query1); 
     $raw_results1 = mysql_query("SELECT * FROM charc_s WHERE `charc_f` ='$query1' ORDER BY FLAG_S") or die(mysql_error()); 
     if(mysql_num_rows($raw_results1) > 0) { 
     while($results1 = mysql_fetch_array($raw_results1)){ 

     echo "<table id='iseqchart' class='sortable'> 
       <tr valign='middle'> 
        <td ><a href='flag.php?FLAG_ID=".$results1['FLAG_ID']."'><img src='images/".$results1['IMAGE']."'></a></td> 
        <td>&nbsp;</td> 
        <td><strong><font size='3'><a href='flag.php?FLAG_ID=".$results1['FLAG_ID']."' style='color:#100783'>".$results1['FLAG_S']."&nbsp;(".$results1['STATE'].")</strong></a>     
        </td> 
       </tr> 
       </table>"; 
      } 
     } 
     else{ // if there is no matching rows do following 
      echo "No results were found"; 
     } 
    } 
    else{ // if query length is less than minimum 
     echo " ".$min_length; 
    } 
?> 

</div> 

<!-- shared characteristics --> 
<div class="fluid sharecharc">Shared</div> 

<!-- state 2 characteristics only --> 
<div class="fluid s2charc"> 
<?php echo "<strong><p>Search results for $query2</strong></p>"; ?> 

<?php if(strlen($query2) >= $min_length){$query2 = htmlspecialchars($query2); 
     $query2 = mysql_real_escape_string($query2); 
     $raw_results = mysql_query("SELECT * FROM charc_s WHERE `charc_f` ='$query2' ORDER BY FLAG_S") or die(mysql_error()); 
     if(mysql_num_rows($raw_results) > 0) { 
     while($results = mysql_fetch_array($raw_results)){ 
     echo "<table id='iseqchart' class='sortable'> 
       <tr valign='middle'> 
        <td ><a href='flag.php?FLAG_ID=".$results['FLAG_ID']."'><img src='images/".$results['IMAGE']."'></a></td> 
        <td>&nbsp;</td> 
        <td><strong><font size='3'><a href='flag.php?FLAG_ID=".$results['FLAG_ID']."' style='color:#100783'>".$results['FLAG_S']."&nbsp;(".$results['STATE'].")</strong></a>     
        </td> 
       </tr> 
       </table>"; 
      } 
     } 
     else{ // if there is no matching rows do following 
      echo "No results were found"; 
     } 
    } 
    else{ // if query length is less than minimum 
     echo " ".$min_length; 
    } 
?> 

</div> 
+2

不知道這是你需要的,但它可能有幫助... http://php.net/manual/de/function.array-diff.php 這個函數會返回你不存在的每個值在兩個陣列中。 –

+0

使用@TanuelMategi建議的內容 - 您應該將結果保存在數組中,然後比較兩者。 –

回答

0

做到這一點,最好的方法是在服務器端。使數據庫做3個不同的選擇。原因是服務器比php運行查詢要快得多。它將以更好的用戶體驗而告終。

只有表A

SELECT * FROM TableA A LEFT JOIN TableB B ON A.key = B.key 

只有表B

SELECT * FROM TableA A RIGHT JOIN TableB B ON A.key = B.key 

而且在這兩個表

SELECT * FROM TableA A INNER JOIN TableB B ON A.key = B.key 

從那裏回聲出結果。