0

我正在从数据库中提取一些数据,它是一个私人站点,所以我现在不太担心使用 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 是最好的选择,那就这样吧,但我想可能有更简单的方法?

//编辑
我更新了代码并基本上添加了整个代码,我意识到我的代码很混乱,我是自学的,只是学到了足够的东西来做我需要做的事情。我敢肯定它可以改得更紧凑,但我的主要问题是即使使用以前的解决方案之一,它仍然没有按异常列的 # 排序,我猜我的解决方案很好省略一些其他代码意味着它不起作用,所以我决定这次包含整个代码。我最初忽略它是为了让它不那么复杂,但我现在意识到这可能会适得其反。

4

2 回答 2

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)

到查询结束。

于 2013-02-18T21:07:57.433 回答
0

在我看来,您可以跳过第一个查询。并在第二个查询中使用 group by 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,这样可以更容易理解您的查询

于 2013-02-18T21:14:24.293 回答