2013-03-19 70 views
1

我遇到了問題,試圖正確格式化我的數據。顯示排序的數據

首先,我的PHP代碼如下所示(用於演示中看到JSFiddle):

$result_rules = $db->query("SELECT rules.source_id, rules.destination_id, dest.project AS dest_project, src.project AS src_project, src.pk_id AS src_id, excep.project AS excep_project, excep.feature AS excep_feature, excep.milestone AS excep_milestone 
    FROM dbo.FFC_Rules rules 
    INNER JOIN dbo.Destination dest 
     ON dest.pk_id=rules.destination_id 
    LEFT JOIN dbo.Source src 
     ON src.pk_id=rules.source_id 
    LEFT JOIN dbo.Exceptions excep 
     ON src.pk_id=excep.source_id 
    ORDER BY dest.project ASC"); 

$last_src = false; 
$last_dest = false; 
while($row = sqlsrv_fetch_array($result_rules)){ 
    if ($row['dest_project'] !== $last_dest) { 
     if ($last_dest !== false) 
      echo "</div>"; 
     $last_dest = $row['dest_project']; 
     echo "<div class='projectscontainer'>"; 
     echo "<div id='arrow' class='arrow-right'></div>"; 
     echo "<span class='item destproject unselectable' title='ID: $row[destination_id]'>$row[dest_project]</span>"; 
     echo "<br>"; 
    } 

    echo  "<div class='srcprojects'>"; 
    if (is_null($row['src_id'])) { 
     echo  " Source ID for Destination ID $row[destination_id] is NULL"; 
    } else { 
     if($row['src_project'] !== $last_src) { 
      $last_src = $row['src_project']; 
      echo "<span class='item srcproject' title='ID: $row[src_id]'>$row[src_project]</span>"; 
      if (!is_null($row['excep_feature'])) 
       echo"<div id='arrow' class='arrow-right'></div><span class='item srcproject exception' title='Feature'>$row[excep_feature]</span>"; 
      if (!is_null($row['excep_milestone'])) 
       echo"<div id='arrow' class='arrow-right'></div><span class='item srcproject exception' title='Milestone'>$row[excep_milestone]</span>"; 
      echo "<br>"; 
     } 
    } 
    echo  "</div>";//end srcprojects 
} 
echo  "</div>";//end projectscontainer 

在我的詢問,我下令目標項目,並在我的PHP代碼,我檢查當前dest爲和最後一樣。如果是這樣,我不顯示名稱。我想爲Source項目做同樣的事情,但由於查詢是按目標項目排序的,所以重複發生在不同的地方,並且不會被我的PHP代碼捕獲。

我該如何做到這一點,而不必再次抓取查詢返回的所有40,000多行?

回答

0

你必須理清一個其他的內部,如:

ORDER BY dest.project ASC,ASC src.project

,並使用LIMIT子句來說明你從查詢多少行希望。