2013-02-18 77 views
0

我從數據庫中拉出一些數據,這是一個私人網站,所以我不太擔心現在使用MySQL,雖然我明白我應該使用PDO,只是沒有' t做了開關呢:在MySQL中設置默認排序從MySQL查詢

<table id="table" border="1" bordercolor="#000000" cellpadding="2" cellspacing="0">   
<thead> 
<tr> 
<th> 
     <span class="th"> 
      <span class="arrow"></span> 
      <span class="icon"></span> 
      <span class="title">Exception ID</span> 
     </span> 
</th> 
<th> 
     <span class="th"> 
      <span class="arrow"></span> 
      <span class="icon"></span> 
      <span class="title">Exception</span> 
     </span> 
</th> 
<th> 
     <span class="th"> 
      <span class="arrow"></span> 
      <span class="icon"></span> 
      <span class="title">First 250 chars of code</span> 
     </span> 
</th> 
<th> 
     <span class="th"> 
      <span class="arrow"></span> 
      <span class="icon"></span> 
      <span class="title"># of exceptions</span> 
     </span> 
</th> 
<th> 
     <span class="th"> 
      <span class="arrow"></span> 
      <span class="icon"></span> 
      <span class="title">Bug #</span> 
     </span> 
</th> 
</tr></thead><tbody> 

    <?php 
//Need to find the total rows in the snippets_link_email_id because well need to show the last x records, so we get total number or rows and then minus the total RECORDS to only select the last x 
$total_snippet_check = mysql_query("SELECT COUNT(email_id) as num_rows FROM snippets_link_email_id"); 
$row = mysql_fetch_object($total_snippet_check); 
$total_rows = $row->num_rows; 


//finding out how many exceptions there was in the last 24 hours from a table that records total exceptions every hour 
$how_many_recent_crashes = mysql_query("SELECT * FROM crash_log_entries ORDER BY crash_id DESC LIMIT 24"); 
    while ($row_recent_crashes = mysql_fetch_array($how_many_recent_crashes)) 
    { 
     $crash_processed = $row_recent_crashes['crash_processed']; 
     $crash_processed_total += $crash_processed; 
    } 

$which_records = $total_rows - $crash_processed_total; 


    //need info on that snippet 
$feedback_query_first = 
    "SELECT *, COUNT(*) AS tot_snippets 
    FROM snippets 
    LEFT JOIN snippets_link_email_id 
    ON (snippets.snippet_id = snippets_link_email_id.snippet_id) 
    WHERE snippets_link_email_id.email_id > $which_records 
    GROUP BY snippets.snippet_id 
    ORDER BY tot_snippets asc"; 

$result1 = mysql_query($feedback_query_first); 

    while ($row1 = mysql_fetch_array($result1)) 
     { 
     $i = $row1['snippet_id']; 
     //Need to find the total snippets for the current snippet 
     $feedback_query = mysql_query(
     "SELECT * FROM snippets 
     LEFT JOIN snippets_link_email_id 
     ON snippets.snippet_id = snippets_link_email_id.snippet_id 
     WHERE snippets_link_email_id.snippet_id = $i 
     AND snippets_link_email_id.email_id > $which_records"); 
     $tot_snippets = mysql_num_rows($feedback_query); 

     $snippet_text_pre = $row1['snippet_text']; 
     $snippet_text_pre1 = htmlspecialchars($snippet_text_pre); 
     $snippet_text = str_replace("&lt;br /&gt;", "<br />",$snippet_text_pre1); 
     $snippet_text = substr($snippet_text,0,250); 

     $comment = $row1['comment']; 
     $comment_short = substr($comment, 0, 35); 
     $note_length = strlen($comment); 
     $snippet_id = $row1['snippet_id']; 
     $email_id = $row1['email_id']; 


     $query_exceptions = "SELECT * FROM emails WHERE email_id = $email_id ORDER BY email_id DESC"; 
     $result2 = mysql_query($query_exceptions); 

     while ($row2 = mysql_fetch_array($result2)) 
     { 
     $actual_exception = $row2['actual_exception']; 
     } 

      echo '<tr><td>'.$snippet_id.'</td>'; 
      echo '<td>'. $actual_exception.'</td>'; 
      echo '<td>'. $snippet_text.'....</td>'; 
      echo '<td>'. $tot_snippets.'</td>'; 
      $tot_tot += $tot_snippets; 
      echo '<td>'. $comment . '</td></tr>'; 

     } 


echo "</tbody></table>"; 

echo "the total exceptions for this time period is: " . $tot_tot . "<br />";  
?> 

好吧,希望這是好的,我濾除了一切無關緊要,所以希望代碼有一定意義。現在有沒有一種方法可以在頁面加載時默認排序爲$tot_snippets,而無需使用完整的jQuery排序解決方案,因爲我只想按此列對此進行排序,因爲我不需要更新它。我不認爲我可以使用orderby來排序,因爲tot_snippets的值不是列值,但我似乎找到的所有解決方案都有一個完整的排序解決方案,並且大多數涉及Jquery,如果Jquery是最好的選擇,我認爲可能有更簡單的方法?

//編輯
我更新了代碼,並添加了基本上整個代碼,我意識到我的代碼很混亂,我自學成才,只是學會了做我需要做的事情。我相信它可以被改變得更加緊湊,但是我的主要問題是即使使用以前的解決方案之一,它仍然不能排序在異常列中,我猜測解決方案的提供對我來說很好遺漏其他一些代碼意味着它不起作用,所以我決定這次包括整個代碼。我最初放棄它以使其更加複雜,但現在我意識到這可能是反效果的。

回答

0

實際上,你可以結合這兩個查詢:

SELECT snippets.snippet_id,COUNT(snippets_link_email_id.id) FROM snippets 
LEFT JOIN snippets_link_email_id ON snippets.snippet_id = snippets_link_email_id.snippet_id 
WHERE snippets_link_email_id.snippet_id = $i 
AND snippets_link_email_id.email_id > $which_records 
GROUP BY snippets.snippet_id 

這意味着你只需要做一個查詢,而不是(numsnippets + 1)查詢。這也允許通過添加計數按次數:

ORDER BY COUNT(snippets_link_email_id.id) 

到查詢結束。

+0

謝謝對於解決方案,我更新了我的代碼,因爲您的解決方案對我而言並不奏效,我懷疑這是因爲我遺漏了代碼的重要部分。 – user1547410 2013-02-19 11:02:59

0

在我看來,你可以跳過第一個查詢。並在第二個查詢 中使用snippet_id的組獲得不同的ID。 然後,您可以添加一個計數到查詢中(我使用別名tot_snippets) ,並通過別名tot_snippets進行排序。 然後你得到這樣的

select *, count(*) as tot_snippets from snippets left join snippets_link_email_id on(snippets.snippet_id = snippets_link_email_id.snippet_id) where snippets_link_email_id.email_id > $which_records group by snippets.snippet_id order by tot_snippets asc

查詢要在表上查詢更具可讀性使用別名和名稱表片斷列snippet_id只是ID這使得它更容易理解您的查詢

+0

謝謝,我沒有100%確定如何應用這個,但我更新了我的代碼,也許它現在更有意義 – user1547410 2013-02-19 11:03:27

+0

確定但tot_snippets排序工作。 – Miguelo 2013-02-22 17:37:10

+0

也許我不清楚,但不要對我提供的查詢執行mysql_num_rows將得到一行vor每個snippet_id與列tot_snippets在它可以顯示您的代碼與我提供的查詢執行,這使得它更清楚我 – Miguelo 2013-02-22 18:03:03