我想编写一个Mysql语句,从表格(发布)中选择所有标题,其中标题是$ title,除了标题为$ title。基本上我想显示某个帖子的所有相关帖子。我希望查询选择表中标题或详细信息中具有标题名称的所有帖子。但我不希望帖子显示在相关帖子中。
//pseudocode
$query="Select * From posting Where title,detail, like %$title% except $title";
如何编写除外部分?
答案 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%'
是一个矛盾。