下面的代码按照“votes_up”的降序排列MySQL数据库中的前25个条目(变量“site”)。它很棒。每行都有一个名为“createddatetime”的列,具有以下结构:
输入:timestamp 默认值:CURRENT_TIMESTAMP
如何更改以下代码以显示最近添加的25个条目?另外,我怎么能显示这样的时间戳:“格林威治标准时间2009年11月1日下午3:45”
提前致谢,
约翰
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"].'`';
}
//$subQuery = "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>";
答案 0 :(得分:1)
试试这个:
... GROUP BY site ORDER BY sum(votes_up) DESC, createddatetime DESC LIMIT 25