从mysql查询中设置php中列的默认排序


Set default sort on column in php from a mysql query

我正在从数据库中提取一些数据,这是一个私人网站,所以我现在不太担心使用mysql,尽管我知道我应该使用PDO,只是还没有切换:

<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是最好的选择,那就这样吧,但我认为可能有更简单的方法?

//编辑
我更新了代码,基本上添加了整个代码,我意识到我的代码很乱,我自学成才,刚刚学会了做我需要做的事情。我相信它可以修改得更紧凑,但我的主要问题是,即使有以前的解决方案,它仍然没有按#异常列排序,我猜给出的解决方案很好,因为我遗漏了一些其他代码,这意味着它不起作用,所以我决定这次包括整个代码。我最初把它放在一边是为了让它不那么复杂,但现在我意识到这可能会适得其反。

您实际上可以将这两个查询结合起来:

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

这意味着您只需要执行一个查询,而不需要执行(numsnippet+1)个查询。这也允许通过添加以下内容按计数排序:

ORDER BY COUNT(snippets_link_email_id.id)

到查询的末尾。

在我看来,您可以跳过第一个查询。并在第二个查询中获取不同的id使用snippet_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命名为just-id,这样可以更容易地理解您的查询