【问题标题】:retrieve distinct sentence that contains words in order按顺序检索包含单词的不同句子
【发布时间】:2015-03-08 14:55:58
【问题描述】:

使用 MySQL,如果您有一个句子表和一个包含它们关系的多对多表的单词表,您将如何构建查询以查找包含短语的句子?

例如,使用这些表...

Words
1 apples
2 like
3 I
4 used
5 to
6 eat

Sentences
1 I used to like apples
2 I used to eat apples

Words2Sentences
pkey fk_word fk_sentence
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 1 2
7 3 2
8 4 2
9 5 2
10 6 2

您将如何构建连接和查询以找到短语“用于”。由于多语言支持要求,全文索引无法满足我的需求。

【问题讨论】:

    标签: mysql search


    【解决方案1】:

    WHERE 子句中使用INNER JOINLIKE。见下文:

    SELECT 
          Words,Sentences
    FROM Words A 
    INNER JOIN Words2Sentences B A.fk_word=B.fk_word
    INNER JOIN Sentences C B.fk_sentence=C.fk_sentence
    WHERE Sentences LIKE '%used to%'
    

    【讨论】:

    • 我不明白这与 SELECT sentence FROM sentence WHERE sentence LIKE '%used to%' 有何不同。我们要寻找的最终结果记录是句子行。此查询将返回类似“The man refUSED TO eat apples”的记录
    【解决方案2】:

    WHERE 子句中使用 INNER JOIN ** with **LIKE

    SELECT ws.pkey,w.words,s.sentences FROM Words2Sentences as ws
    INNER JOIN Words as w ON w.fk_word = ws.fk_word
    INNER JOIN Sentences as s ON s.fk_sentence = ws.fk_sentence
    WHERE s.sentences RLIKE '%[[:>:]] used to [[:<:]]%' GROUP BY ws.pkey ORDER BY ws.pkey
    

    【讨论】:

    • 我不明白这与 SELECT sentence FROM sentence WHERE sentence LIKE '%used to%' 有何不同。我们要寻找的最终结果记录是句子行。此查询将返回类似“The man refUSED TO eat apples”的记录。
    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多