2014-02-20 67 views
0

我有一个MySQL表,它记录每个访客的搜索条件。有时它是一个单词,有时它是多个单词。在我的后端,我想列出搜索最多的单词desc limit x搜索词最多的词MYSQL

当然,我可以用PHP做,但我想知道,如果它也可以与MySQL。

search_string 
-------------- 
Hotel in Berlin 
Paris 
New York 
Grand Hotel Principe 
Royal Myconian Resort Thalasso Spa 
Spa Resort 

我知道像纽约这样的城市可以分为纽约和纽约,但忽略这一点。

感谢您的帮助!

+1

使用全文搜索:https://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html – PravinS

+0

1.打开浏览器,2.导航到google.com 3.搜索mysql group by count or something 4.根据你的发现编写代码 –

+0

感谢PravinS。埃利亚斯和约书亚的另一个问题。评论是否给予了声誉,或者为什么你花时间写出无用的和令人反感的(Elias)答案? – BastianW

回答

3

这是可能做到这一点在一个单一的查询,但你必须承担的话的最大数量。

select word, count(*) as cnt 
from (select substring_index(substring_index(search_string, ' ', n.n), ' ', -1) as word 
     from searches s cross join 
      (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 
      ) n 
     where length(replace(search_string, ' ', ' x')) - length(search_string) <= n 
    ) w 
group by word 
order by cnt desc; 

substring_index()的嵌套调用返回列表中的“第n个”字。条款where验证了许多单词在列表中。

+0

单词的数量是动态的,但我会努力解决您的问题。谢谢 – BastianW