检索包含单词的不同句子

时间:2015-03-07 00:34:56

标签: mysql search

使用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

如何构建连接和查询以便找到短语"用于"。由于多语言支持要求,全文索引不能满足我的需求。

2 个答案:

答案 0 :(得分:0)

INNER JOIN子句中使用LIKEWHERE。见下文:

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%'

答案 1 :(得分:0)

WHERE 条款

中使用 INNER JOIN **和** 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