我从数据库中提取一些数据,这是一个私人网站所以我现在不太担心使用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("<br />", "<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 :(得分: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)
到查询结尾。
答案 1 :(得分:0)
在我看来,您可以跳过第一个查询。并在第二个查询中获取不同的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命名为id,这样可以更轻松地理解您的查询