DEDE高效随机文章数据库优化代码

DEDE 2019-10-13 21:40:55 评论
{dede:php}
header("Content-type:text/html;charset=utf-8");
$servername = "localhost";//MySQL默认为localhost,端口号3306
$username = "数据库账号";
$password = "数据库密码";
$dbname = "数据库名";
 
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
 
$sql = "SELECT * 
FROM `dede_archives` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `dede_archives`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id ASC LIMIT 10";
$result = $conn->query($sql);
 
if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo '<li><a href="https://www.657k.com/article-'.$row['id'].'-1.html" title="'.$row['title'].'" target="_blank">'.$row['title'].'</a></li>';
    }
} else {
    echo "0 结果";
}
$conn->close();
{/dede:php}


{dede:sql sql="SELECT * FROM `dede_archives` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `dede_archives`)-(SELECT MIN(id) FROM `dede_archives`))+(SELECT MIN(id) FROM `dede_archives`)) AS 5id) AS t2 WHERE t1.id > t2.5id ORDER BY t1.id LIMIT 5;"}
<li><a href="https://www.huaianseo.cn/article-[field:id/]-1.html" title="[field:title/]" >[field:title/]</a></li>
{/dede:sql}

 

评论

说点什么吧
  • 全部评论(0
    还没有评论,快来抢沙发吧!