PostgreSQL统计关键字匹配中每个关键字的频率

问题描述 投票:0回答:1

基本上,我正在检查列是否包含数组中的任何关键字列表

SELECT COUNT(*) FROM table WHERE value ILIKE ANY (ARRAY['%bat%', '%cat%', ...])

这可以很好地为我提供这些关键字的列总数,但我还想计算每个关键字被点击的次数。理想情况下我最终会得到

 Keyword   |   Count
---------------------
 bat       |     4
 cat       |    10

等等。有任何想法吗?谢谢!

postgresql postgresql-9.4
1个回答
2
投票

您最好使用正则表达式来测试文本中的完整单词:

with
  words(word) as (values ('bat'), ('cat'))

select w.word, count(*)
from words w
join table t on (t.value ~* ('\m'||w.word||'\M'))
group by w.word;

如果您需要区分大小写的搜索,请将

~*
替换为
~

© www.soinside.com 2019 - 2024. All rights reserved.