匹配的全文搜索 - 将BY与工作相关联

时间:2015-07-06 03:39:15

标签: mysql

我有以下查询,但尝试添加ORDER BY relevance DESC。任何人都可以告诉我正确的语法,因为我没有成功地将其用于工作并且没有得到匹配的结果:

$query =
"(SELECT postid as id, title as name, 'post' as type FROM post WHERE MATCH (title, content) AGAINST ('$keywords' IN BOOLEAN MODE))          
UNION 
(SELECT pdid as id, pdname as name, 'product' as type FROM product WHERE MATCH (pdname, pddesc, pddetail) AGAINST ('$keywords' IN BOOLEAN MODE))";

这就是我的尝试:

$query =
"(SELECT postid as id, title as name, 'post' as type MATCH (title, content) AGAINST ('$keywords' IN BOOLEAN MODE) as relevance FROM post
WHERE MATCH (title, content) AGAINST ('$keywords' IN BOOLEAN MODE))         
UNION 
(SELECT pdid as id, pdname as name, 'product' as type MATCH (pdname, pddesc, pddetail) AGAINST ('$keywords' IN BOOLEAN MODE) as relevance FROM product
WHERE MATCH (pdname, pddesc, pddetail) AGAINST ('$keywords' IN BOOLEAN MODE))
ORDER BY relevance DESC
";

1 个答案:

答案 0 :(得分:1)

$query =
"(SELECT postid as id, title as name, 'post' as type, MATCH (title, content) AGAINST ('$keywords' IN BOOLEAN MODE) as relevance FROM post
WHERE MATCH (title, content) AGAINST ('$keywords' IN BOOLEAN MODE))         
UNION 
(SELECT pdid as id, pdname as name, 'product' as type, MATCH (pdname, pddesc, pddetail) AGAINST ('$keywords' IN BOOLEAN MODE) as relevance FROM product
WHERE MATCH (pdname, pddesc, pddetail) AGAINST ('$keywords' IN BOOLEAN MODE))
ORDER BY relevance DESC
";

我在'as type'之后发现了一个缺少的逗号。版本之后,现在工作正常。

问题解决了,并且运作良好。