你如何写“选择(所有)从(表)哪里发布= $发布除了这个帖子?(Mysql)

时间:2010-04-10 03:46:58

标签: php mysql

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

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

如何编写除外部分?

2 个答案:

答案 0 :(得分:2)

这是您需要的代码,但如果您拥有当前的帖子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 . "
    ";

答案 1 :(得分:0)

编辑:如果你想要'%$ title%'和!='$ title'那么:

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

否则

SELECT * FROM posting 
WHERE title LIKE '%$title%' AND detail LIKE '%$title%' AND title NOT LIKE '%$title%';

你确定这是对的吗? LIKE '%$title%' AND NOT LIKE '%$title%'是一个矛盾。