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 />";    
?>

関係のないものはすべて除外したので、コードが意味をなすことを願っています。完全なjqueryソートソリューションを使用せずにページがロードされたときにデフォルトでソートする方法はあり$tot_snippetsますか?更新する必要がないので、常にこの列でソートしたいだけですか? orderbyの値は列の値ではないため、これを使用してソートできるとは思いませんtot_snippetsが、私が見つけたように見えるすべてのソリューションには本格的なソートソリューションがあり、Jqueryが最適なオプションである場合はほとんどJqueryが関係していますが、私は考えましたもっと簡単な方法があるかもしれませんか?

//編集
コードを更新し、基本的にコード全体を追加しました。自分のコードが乱雑であることに気付きました。独学で、必要なことを行うのに十分なだけ学習しました。もっとコンパクトになるように変更できると確信していますが、私の主な問題は、以前のソリューションの 1 つを使用しても、例外の数列でソートされないことです。他のコードの一部を除外すると機能しないため、今回はコード全体を含めることにしました。複雑さを軽減するために最初は省略しましたが、おそらく逆効果であることに気づきました。

4

2 に答える 2

0

これら 2 つのクエリを実際に組み合わせることができます。

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) クエリではなく、1 つのクエリを実行するだけで済みます。これにより、次を追加することでカウントによる順序付けも可能になります。

ORDER BY COUNT(snippets_link_email_id.id)

クエリの最後まで。

于 2013-02-18T21:07:57.433 に答える
0

私の意見では、最初のクエリをスキップできます。snippet_id によるグループを使用して、2 番目のクエリで個別の 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 という名前を付けます。これにより、クエリが理解しやすくなります。

于 2013-02-18T21:14:24.293 に答える