我创建了一个SQL语句,该语句应该返回按相关性排序的项目列表,这些项目是提供的三个单词。 (TAG_1,TAG_2等)。这是代码:
$sql = mysql_query("SELECT * FROM SEARCH_POSTS WHERE(
POST_TAG_1 LIKE '%{$_POST['TAG_1']}%'
OR POST_TAG_1 LIKE '%{$_POST['TAG_2']}%'
OR POST_TAG_1 LIKE '%{$_POST['TAG_3']}%'
OR POST_TAG_2 LIKE '%{$_POST['TAG_1']}%'
OR POST_TAG_2 LIKE '%{$_POST['TAG_2']}%'
OR POST_TAG_2 LIKE '%{$_POST['TAG_3']}%'
OR POST_TAG_3 LIKE '%{$_POST['TAG_1']}%'
OR POST_TAG_3 LIKE '%{$_POST['TAG_2']}%'
OR POST_TAG_3 LIKE '%{$_POST['TAG_3']}%'
) ORDER BY(
CASE
WHEN POST_TAG_1 LIKE '%{$_POST['TAG_1']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_1 LIKE '%{$_POST['TAG_2']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_1 LIKE '%{$_POST['TAG_3']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_2 LIKE '%{$_POST['TAG_1']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_2 LIKE '%{$_POST['TAG_2']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_2 LIKE '%{$_POST['TAG_3']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_3 LIKE '%{$_POST['TAG_1']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_3 LIKE '%{$_POST['TAG_2']}%' THEN 1
ELSE 0
END CASE +
CASE
WHEN POST_TAG_3 LIKE '%{$_POST['TAG_3']}%' THEN 1
ELSE 0
END CASE) DESC");
WHERE子句工作正常,但看起来ORDER BY(...)给了我一个错误。我究竟做错了什么?一切看起来都是有序的。谢谢。
答案 0 :(得分:2)
END CASE
应为END
。 END CASE
用于结束存储过程中的CASE
语句,它不用于结束CASE
表达式。
答案 1 :(得分:1)
您正在使用不需要的case
。试试这个order by
ORDER BY( CASE WHEN POST_TAG_1 LIKE '%{$_POST['TAG_1']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_1 LIKE '%{$_POST['TAG_2']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_1 LIKE '%{$_POST['TAG_3']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_2 LIKE '%{$_POST['TAG_1']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_2 LIKE '%{$_POST['TAG_2']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_2 LIKE '%{$_POST['TAG_3']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_3 LIKE '%{$_POST['TAG_1']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_3 LIKE '%{$_POST['TAG_2']}%' THEN 1 ELSE 0 END +
CASE WHEN POST_TAG_3 LIKE '%{$_POST['TAG_3']}%' THEN 1 ELSE 0 END ) DESC