gpt4 book ai didi

MySQL 多行全文搜索

转载 作者:行者123 更新时间:2023-11-29 19:13:29 27 4
gpt4 key购买 nike

我有一个名为essays 的表,它与一个名为paragraphs 的表具有一对多的关系。因此,paragraphs 表具有指向 essay 的外键。

我当前遇到的问题是,如果我进行搜索,MySQL 将查看每个单独的段落,但不知道多个段落可以指向同一本书。

我的全文搜索如下所示:

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

有没有办法让 MySQL 在一篇文章的所有段落中搜索某些内容并返回该文章?

最佳答案

这样的事情对你有用吗?

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;

您还可以使用 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;

请注意,在使用 GROUP BY 时不应使用 SELECT *,因为可能会出现不需要的聚合问题。

关于MySQL 多行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42893194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com