Displaying Top 25 Table Rows by Timestamp
The code below lists the top 25 entries (the variable "site") from a MySQL database by "votes_up" in descending order. It works great. Each row has a column called "createddatetime" that is of the following structure:
Type: timestamp Default: CURRENT_TIMESTAMP
How can I change the code below to show the 25 most recently added entries? Also, how could I display the timestamp like this: "1 November 2009 3:45 pm Greenwich Mean Time"
Thanks in advance,
John
mysql_connect("mysqlv8", "username", "password") or die(mysql_error());
mysql_select_db("sitefeather") or die(mysql_error());
$result = mysql_query("SHOW TABLES");
$tables = array();
while ($row = mysql_fetch_assoc($result)) {
$tables[] = '`'.$row["Tables_in_sitefeather"].'`';
}
//$sub开发者_Python百科Query = "SELECT site, votes_up FROM ".implode(" UNION ALL SELECT site, votes_up FROM ",$tables);
$subQueryParts = array();
foreach($tables as $table)
$subQueryParts[] = "SELECT site, votes_up FROM $table WHERE LENGTH(site)";
$subQuery = implode(" UNION ALL ", $subQueryParts);
// Create one query that gets the data you need
$sqlStr = "SELECT site, sum(votes_up) sumVotesUp
FROM (
".$subQuery." ) subQuery
GROUP BY site ORDER BY sum(votes_up) DESC LIMIT 25";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samples2\">";
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td class="sitename2"><a href="sitelookup3.php?entry='.urlencode($row["site"]).'&searching=yes&search=search">'.$row["site"].'</a></td>';
echo '<td>'.$row["sumVotesUp"].'</td>';
echo '</tr>';
}
echo "</table>";
Try this:
... GROUP BY site ORDER BY sum(votes_up) DESC, createddatetime DESC LIMIT 25
精彩评论