我需要获取 3 个最近帖子的标题,但如果它是最近的 3 个帖子之一,则省略一个特定的帖子。
我懂了:
SELECT
postID,
title
FROM
posts
WHERE
categoryID = $categoryID
ORDER BY
date DESC
LIMIT
3
这很好用,但我需要告诉是否要省略 "postID = $postID" "$postID" 是不应该显示并且之前定义的帖子的行。
谢谢!
向子句添加另一个条件以where
过滤特定的postId
SELECT
postID,
title
FROM
posts
WHERE
categoryID = $categoryID
AND
postID <> $postID
ORDER BY
date DESC
LIMIT
3
这应该可以解决问题:
SELECT
postID,
title
FROM
posts
WHERE
categoryID = $categoryID
AND
postID != $postID
ORDER BY
date DESC
LIMIT
3
我对你的理解正确吗?您只想省略等于 $postID 的 postID?
SELECT
postID,
title
FROM
posts
WHERE
categoryID = $categoryID
AND
postID <> $postID
ORDER BY
date DESC
LIMIT
3
此查询使用 <> 运算符返回预期结果:
SELECT
postID,
title
FROM
posts
WHERE
categoryID = $categoryID
AND postID <> $postID
ORDER BY
date DESC
LIMIT
3