选择不同的字数?

时间:2014-03-11 20:56:58

标签: mysql select count

我正在尝试整理一份报告,显示用户使用的搜索条件。

现在它是基本的,使用SELECT DISTINCT然后使用GROUP BY。

问题是我们希望看到这个也被文字分解。短语标准很有用,但我们希望看到:

Searches:
red apples are good
yellow bananas are bad
bad apples are not bananas
pears are not red

What we would like to see:
red            2
apples         2
are            4
good           1
yellow         1
bananas        2
bad            2
pears          1
not            2

我也应该注意,我们也有搜索条件来为它们编写单独的%LIKE语句 - 然后它们会改变。

1 个答案:

答案 0 :(得分:2)

试试这个例子

SELECT word,COUNT(*) as count
FROM
(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(searches,' '),' ',value.v),' ',-1) as word
FROM yourtable,(SELECT 1 as v UNION 
                SELECT 2 UNION 
                SELECT 3 UNION
                SELECT 4 UNION
                SELECT 5)value
 )T
WHERE word != ''
GROUP BY word

sqlFiddle