MySQL不选择一行

时间:2013-03-28 17:22:24

标签: php mysql select where

我需要获得最近3篇帖子的标题,但如果它出现在最近的3个帖子之一中,则省略一个特定的帖子。

我明白了:

SELECT
  postID,
  title
FROM
  posts
WHERE
  categoryID = $categoryID
ORDER BY
  date DESC
LIMIT
  3

这样可以正常工作,但是我需要判断是否省略了“postID = $ postID”的行 “$ postID”是不应显示并在之前定义的帖子。

谢谢!

4 个答案:

答案 0 :(得分:2)

where子句添加其他条件以过滤特定的postId

SELECT
  postID,
  title
FROM
  posts
WHERE
  categoryID = $categoryID
AND
  postID <> $postID
ORDER BY
  date DESC
LIMIT
  3

答案 1 :(得分:1)

这应该可以解决问题:

  SELECT
      postID,
      title
    FROM
      posts
    WHERE
      categoryID = $categoryID
    AND
      postID != $postID
    ORDER BY
      date DESC
    LIMIT
      3

答案 2 :(得分:0)

我理解正确吗?你只想省略等于$ postID的postID?

SELECT
  postID,
  title
FROM
  posts
WHERE
  categoryID = $categoryID
AND
  postID <> $postID
ORDER BY
  date DESC
LIMIT
  3

答案 3 :(得分:0)

此查询使用&lt;&gt;返回预期结果操作者:

SELECT
  postID,
  title
FROM
  posts
WHERE
  categoryID = $categoryID
  AND postID <> $postID
ORDER BY
  date DESC
LIMIT
  3