SQL查询查找所有两个单词组合

时间:2015-07-02 23:17:28

标签: php mysql

使用PHP和MySQL我需要找到所有"两个字"从具有超过一百万行的表中按计数排序的组合。

搜索需要找到每两个单词组合的实例数,例如"硬件商店","到","第二次机会",&# 34;对于那些","老年人"等

要搜索的文字示例:

id | content
1  | The senior citizens went to the hardware store from the community center.

2  | The hardware store is offering a second chance drawing for those senior citizens who did not win.

3  | Many senior citizens go to the town's community center to play bingo.

示例结果:

senior citizens - 3
to the - 2
hardware store - 2
community center - 2
second chance - 1
The senior - 1
center to - 1
the town's - 1
etc ...and so on.

结果需要包括所有"两个字"组合。 "老人",去了#34;,"硬件",公民去"等等,以及发现了多少次。

我猜这可能是一个带有子查询的多查询解决方案,但我的查询构建专业知识很少。我已经尝试了一些基本的查询,但我认为解决方案会比我的技能集更复杂。

Similar question with different data source

1 个答案:

答案 0 :(得分:0)

尝试联盟所有加入:

SELECT count(*) FROM your_table WHERE content LIKE '%senior citizens%'
UNION ALL
SELECT count(*) FROM your_table WHERE content LIKE '%to the%'
UNION ALL
SELECT count(*) FROM your_table WHERE content LIKE '%hardware store%'