我正在尝试制作文章网站,但我想规范化当前数据库。我有四个表格,每个表格都标记一个类别,但一个除外四个表格是:娱乐,生活方式,科学和文章。文章是娱乐,生活方式和科学所有条目的组合。我想删除娱乐,生活方式和科学,只留下文章表,从而节省空间和提高性能。我遇到的唯一问题是使用通过获取表格的最后一个ID生成随机文章的查询,然后获取介于6和最大ID之间的随机数。正在更新查询,以反映规范化数据库
我所有的表具有以下结构:id
(唯一的),category
(文章类型这是,即娱乐),title
(文章标题),image
(文章的图片URL),link
(URL ),Counter
(本文观看次数)和dateStamp
(文章发表日期)。以下是我试图根据规范化数据库进行更新的查询。
<?php
//SELECT the MAX id from the Entertainment table
$MAX_ID = $db->query("SELECT MAX(id) FROM Entertainment");
//Get Max id value
$MAX_ID = $MAX_ID->fetch_array();
$MAX_ID = $MAX_ID[0];
//Create random number variable
$RAND_NUM;
//If the Max ID is less than 6, make $MAX_ID the $RAND_NUM
if ($MAX_ID < 6) {
$RAND_NUM = $MAX_ID;
}
//Else get a random value between 6 and the Max ID
else {
$RAND_NUM = mt_rand(6, $MAX_ID);
}
//Grab 6 articles by descending from the random number, example: If $RAND_NUM is 7, get all entries from 7-2
$resultSet = $db->query("SELECT * FROM Entertainment WHERE id <= $RAND_NUM ORDER BY id DESC LIMIT 6");
if ($resultSet->num_rows != 0) {
//Booleans to check where we are at in the print off
$conditional = true;
$conditional2 = true;
echo "<div class='row'>";
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$title = $rows["title"];
$link = $rows["link"];
$count = number_format($rows["Counter"]);
//Print off these articles on the left
if ($conditional == true && $conditional2 == true) {
echo "<div class='left'><a href='$link'><img src=$image><p>$title</p></a><p id='article-views'><span class='glyphicon glyphicon-fire'></span> $count Views</p></div>";
$conditional2 = false;
}
//Print off these articles on the right
else {
echo "<div class='right'><a href='$link'><img src=$image><p>$title</p></a><p id='article-views'><span class='glyphicon glyphicon-fire'></span> $count Views</p></div><hr>";
$conditional2 = true;
}
}
echo "</div>";
}
?>
我应该如何更改/更新此查询,以便从文章表中获得随机娱乐文章?我愿意接受任何改善查询性能的改变。
仅供参考,您的旧方法假定低编号的文章编号从未删除。 – Barmar