SQL:如何获取一列句子的行中所有字符串的总和

时间:2019-04-15 14:14:57

标签: sql

Archive the artifacts

是否有一个SQL查询能够将所有句子拆分为字符串,对它们进行计数并返回前3个最常用的字符串?

例如:-

|                        random_column                         |
|---------------------------------------------------------------|
| The magnetic quality of a sample of iron depends on the purity|
| A sample is taken from each bale separately.                  |
| I love Switzerland                                            |
| Be sure to sample the chocolate-walnut torte                  |
| Mary had a little lamb                                        |
| This is a very cool table                                     |

1 个答案:

答案 0 :(得分:1)

在MySQL中,您可以使用以下内容来解析单词:

select substring_index(substring_index(col, ' ', n.n), ' ', -1) as word
from (select 1 as n union all select 2 as n union all select 3 as n
     ) n join
     t
     on col like concat(repeat('% ', n.n - 1), '%');

请注意,此版本仅获取前三个单词。您需要将n扩展为该列中的最大单词数。

然后您可以将其汇总为:

select substring_index(substring_index(t.col, ' ', n.n), ' ', -1) as word,
       count(*)
from (select 1 as n union all select 2 as n union all select 3 as n
     ) n join
     t
     on col like concat(repeat('% ', n.n - 1), '%')
group by word