gpt4 book ai didi

php - 有没有办法检查结果是表中的最后一个(最高 ID)?

转载 作者:行者123 更新时间:2023-11-30 22:34:00 27 4
gpt4 key购买 nike

我有一个用于显示博客文章的页面。在页面的开头,我使用以下 SQL 获取数据:

SELECT posts.*, count(comments.post_id) as number_of_comments from posts left     join comments on (posts.post_id = comments.post_id) WHERE post_url LIKE '$post_url' group by posts.post_id

我在页面末尾添加了一个按钮来“阅读下一篇文章”,按下该按钮后,会找到当前显示的文章的 ID,然后从数据库中获取下一篇文章:

$(".readNext").click(function(){
var result = "";
var postID = "postID=" + $(".post").attr("id");
$.get("/retrieveNextPost", postID, function (data) {
//SomeFunction(data);
window.location = '/blog/' + data;
});
});

<?php
include('dbconnect.php');

if (isset($_GET['postID']))
{
$post_id = $_GET['postID'];
}

$SQL = "SELECT posts.*, count(comments.post_id) as number_of_comments from posts left join comments on (posts.post_id = comments.post_id) WHERE posts.post_id > $post_id group by posts.post_id ORDER BY post_id ASC LIMIT 0, 1";
$result = mysqli_query($link, $SQL);
$row = mysqli_fetch_array($result);
echo $row['post_url'];
?>

这很好用 - 直到您看到最后一篇(最新/最高 ID)博文。在这一点上 - 我想从页面上完全删除按钮。因为显示它没有意义。

有没有一种方法可以修改页面顶部的 SQL 以标记某种 Y/N 标志,如果为真,我可以使用它来隐藏按钮?

即如果当前博客文章记录是表中的最后一个/最高 ID,则将标志设置为 true。

最佳答案

您可以向包含最大 post_id 的帖子添加另一个左连接。如果匹配(不为空),那么您处于最高职位。

在我的脑海中:

SELECT posts.*,
count(comments.post_id) as number_of_comments,
-- if post_id matches then is highest
case when maxPosts.post_id is not null then 1 else 0 end as IsLast
from posts
left join comments on (posts.post_id = comments.post_id)
left join (
-- get max post_id and join on current post_id
select post_id from posts order by post_id desc Limit 1
) maxPosts on post.post_id = maxPosts.post_id
WHERE post_url LIKE '$post_url' group by posts.post_id

关于php - 有没有办法检查结果是表中的最后一个(最高 ID)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com