防止联合全文搜索中的重复结果

Preventing duplicate results in a joined fulltext search

我有两个 table,分别是 post 和信息,每个 post 可以有多行信息。

table 结构如下所示,

post (int id, int published, varchar title)

info (int id, text content, int post_id)

我想在 info 和 return 列表中进行简单的全文搜索 post,但我希望 post 行在列表中只显示一次结果的结果,即使它多次在信息中找到查询文本,

我当前的查询看起来像这样,但它仍然 return 具有相同 ID 的多个 post 行,

SELECT 
    DISTINCT(post.id),
    post.title,
    info.content
FROM
    post, info
WHERE 
    info.post_id = post.id 
    AND published = 1
    AND MATCH(info.content) AGAINST('cucumber')

在 mysql 中,distinct 子句适用于行而不适用于单个列值

如果您只需要一个结果,您应该使用聚合函数并按

分组
    SELECT 
        post.id,
        min(post.title),
        min(info.content)
    FROM post
    INNER  info ON  info.post_id = post.id 
        AND published = 1
    WHERE MATCH(info.content) AGAINST('cucumber')
    group by post.id