我的博客/标签有3张表
lm_blog,lm_blog_tags和lm_blog_tag_relationships。
我现在正在尝试添加使用布尔搜索的搜索功能。我没那么好,但我也无法搜索标签。
SELECT blog.blog_id, title, url, summary,
GROUP_CONCAT(tags.tag SEPARATOR ':') AS tags,
MATCH(title) AGAINST ($query IN BOOLEAN MODE) AS title_rel,
MATCH(summary) AGAINST ($query IN BOOLEAN MODE) AS summary_rel,
MATCH(article) AGAINST ($query IN BOOLEAN MODE) AS article_rel
FROM lm_blog AS blog
INNER JOIN lm_blog_tag_relationships AS rel
ON blog.blog_id = rel.blog_id
INNER JOIN lm_blog_tags AS tags
ON rel.tag_id = tags.tag_id
WHERE MATCH(title, summary, article) AGAINST ($query IN BOOLEAN MODE)
GROUP BY blog.blog_id
ORDER BY title_rel DESC, summary_rel DESC, article_rel DESC, blog.updated DESC
我尝试添加另一个WHERE
子句来匹配聚合标记,但是你不能在where子句中使用组数据。据我所知,没有办法做WHERE OR HAVING
......
这是我得到的最接近但是它会检查WHERE和HAVING。我需要一个WHERE或HAVING
SELECT blog.blog_id, title, url, summary,
GROUP_CONCAT(tags.tag SEPARATOR ':') AS tags,
MATCH(title) AGAINST ($query IN BOOLEAN MODE) AS title_rel,
MATCH(summary) AGAINST ($query IN BOOLEAN MODE) AS summary_rel,
MATCH(article) AGAINST ($query IN BOOLEAN MODE) AS article_rel
FROM lm_blog AS blog
INNER JOIN lm_blog_tag_relationships AS rel
ON blog.blog_id = rel.blog_id
INNER JOIN lm_blog_tags AS tags
ON rel.tag_id = tags.tag_id
WHERE MATCH(title, summary, article) AGAINST($query IN BOOLEAN MODE)
GROUP BY blog.blog_id
HAVING tags LIKE $query
ORDER BY title_rel DESC, summary_rel DESC, article_rel DESC, blog.updated DESC
答案 0 :(得分:0)
经过几个小时的搜索后找到答案......实际上很容易。只需选择带有标签的一个,然后选择一个带有全文搜索并使用UNION DISTINCT加入它们。
SELECT blog.blog_id, title, url, summary, updated,
GROUP_CONCAT(tags.tag SEPARATOR ':') AS tags,
MATCH(title) AGAINST ($query IN BOOLEAN MODE) AS title_rel,
MATCH(summary) AGAINST ($query IN BOOLEAN MODE) AS summary_rel,
MATCH(article) AGAINST ($query IN BOOLEAN MODE) AS article_rel
FROM lm_blog AS blog
INNER JOIN lm_blog_tag_relationships AS rel
ON blog.blog_id = rel.blog_id
INNER JOIN lm_blog_tags AS tags
ON rel.tag_id = tags.tag_id
WHERE MATCH(title, summary, article) AGAINST ($query IN BOOLEAN MODE)
GROUP BY blog.blog_id
UNION DISTINCT
SELECT blog.blog_id, title, url, summary, updated,
GROUP_CONCAT(tags.tag SEPARATOR ':') AS tags,
MATCH(title) AGAINST ($query IN BOOLEAN MODE) AS title_rel,
MATCH(summary) AGAINST ($query IN BOOLEAN MODE) AS summary_rel,
MATCH(article) AGAINST ($query IN BOOLEAN MODE) AS article_rel
FROM lm_blog AS blog
INNER JOIN lm_blog_tag_relationships AS rel
ON blog.blog_id = rel.blog_id
INNER JOIN lm_blog_tags AS tags
ON rel.tag_id = tags.tag_id
GROUP BY blog.blog_id
HAVING tags REGEXP $query
ORDER BY title_rel DESC, summary_rel DESC, article_rel DESC, updated DESC
看起来我不能使用HAVING ... MATCH ... AGAINST ...
所以我会使用正则表达式直到找到解决方案。