[英]MySQL fulltext search over multiple rows
I have a table called essays
which has a 1-to-M relationship with a table called paragraphs
. 我有一个名为essays
的表,该表与一个称为paragraphs
的表具有1-to-M关系。 Therefore, the paragraphs
table has foreign key pointing to essay
. 因此, paragraphs
表具有指向essay
外键。
The current problem I have is that, if I make a search, MySQL will look at each individual paragraph, but has no idea that multiple paragraphs can point to the same book. 我当前遇到的问题是,如果进行搜索,MySQL将查看每个单独的段落,但不知道多个段落可以指向同一本书。
My fulltext search looks like this: 我的全文搜索如下:
SELECT DISTINCT *,
MATCH(essays.title) AGAINST('my search') as tscore,
MATCH(paragraphs.content) AGAINST('my search') as cscore
FROM essays
INNER JOIN paragraphs ON paragraphs.essay_id = essays.id
WHERE
MATCH(essays.title) AGAINST('my search')
OR MATCH(paragraphs.content) AGAINST('my search')
ORDER BY (3 * tscore + cscore) DESC
Is there a way to make MySQL search for something across all the paragraphs of an essay and just return that essay? 有没有一种方法可以使MySQL在论文的所有段落中搜索某些东西,然后只返回该论文?
Does something like this work for you? 这样的事情对您有用吗?
SELECT essays.id, essays.title,
MATCH(essays.title) AGAINST('my search') as tscore,
MATCH(paragraphs.content) AGAINST('my search') as cscore
FROM essays
INNER JOIN paragraphs ON paragraphs.essay_id = essays.id
GROUP BY essays.id
HAVING tscore > 0 OR cscore > 0
ORDER BY (3 * tscore + cscore) DESC;
You could also use WHERE clause instead of HAVING, but then you need to: 您还可以使用WHERE子句代替HAVING,但是您需要:
SELECT essays.id, essays.title,
MATCH(essays.title) AGAINST('my search') as tscore,
MATCH(paragraphs.content) AGAINST('my search') as cscore
FROM essays
INNER JOIN paragraphs ON paragraphs.essay_id = essays.id
WHERE
MATCH(essays.title) AGAINST('my search')
OR MATCH(paragraphs.content) AGAINST('my search')
GROUP BY essays.id
ORDER BY (3 * tscore + cscore) DESC;
Note that you should not use SELECT *
when using GROUP BY
because of unwanted aggregation issues that might pop up. 请注意,使用GROUP BY
时不应使用SELECT *
,因为可能会弹出不必要的聚合问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.