我有一个 mysql 表“post”:
id Post
-----------------------------
1 Post Testing
2 Post Checking
3 My First Post
4 My first Post Check
我需要计算所有值中不同单词的数量Post
column.
有没有办法使用单个查询获得以下结果?
post count
------------------
Post 4
Testing 1
checking 1
My 2
first 2
check 1
不是以一种简单的方式。如果您知道最大单词数,那么您可以执行以下操作:
select substring_index(substring_index(p.post, ' ', n.n), ' ', -1) as word,
count(*)
from post p join
(select 1 as n union all select 2 union all select 3 union all select 4
) n
on length(p.post) - length(replace(p.post, ' ', '')) < n.n
group by word;
请注意,这仅在单词由单个空格分隔时才有效。如果您有一个包含所有可能单词的单独字典,您也可以使用它,例如:
select d.word, count(p.id)
from dictionary d left join
posts p
on concat(' ', p.post, ' ') like concat(' %', d.word, ' %')
group by d.word
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)