php – 你怎么写“Select(all)From(table)Where where = $posted除了这个帖子?(Mysql)

我想编写一个Mysql语句,从表(发布)中选择所有标题,其中标题是$title,除了$title的标题.基本上我想显示某个帖子的所有相关帖子.我希望查询选择表中标题或详细信息中具有标题名称的所有帖子.但我不希望帖子显示在相关帖子中.

//pseudocode
$query="Select * From posting Where title,detail, like %$title% except $title";

我怎么写除了部分?

最佳答案 这是你需要的代码,虽然如果你有当前的帖子ID并且只是像WHERE id!=“.(int)$current_post_id那样会更好.”

$title = mysql_real_escape_string($title);

$sql = "
    SELECT *
    FROM posting
    WHERE
        title LIKE '%" . $title . "%' AND
        detail LIKE '%" . %title . "%' AND
        title != " . $title . "
    ";

这是ID版本,方式更好:)

$title = mysql_real_escape_string($title);

$sql = "
    SELECT *
    FROM posting
    WHERE
        title LIKE '%" . $title . "%' AND
        detail LIKE '%" . %title . "%' AND
        id != " . (int)$post_id . "
    ";
点赞