gpt4 book ai didi

mysql - 计算表列中的单词出现次数

转载 作者:可可西里 更新时间:2023-11-01 07:07:35 28 4
gpt4 key购买 nike

我有一个带有 varchar(255) 字段的表。我想(通过查询、函数或 SP)从该表中的一组行中获取每个单词的出现次数。

如果有 2 行包含这些字段:

"I like to eat bananas""I don't like to eat like a monkey"

我想得到

    word | count()    ---------------    like  3    eat   2    to    2    i     2    a     1

有什么想法吗?我正在使用 MySQL 5.2。

最佳答案

@Elad Meidar,我喜欢你的问题,我找到了解决方案:

SELECT SUM(total_count) as total, value
FROM (

SELECT count(*) AS total_count, REPLACE(REPLACE(REPLACE(x.value,'?',''),'.',''),'!','') as value
FROM (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(t.sentence, ' ', n.n), ' ', -1) value
FROM table_name t CROSS JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n
) n
WHERE n.n <= 1 + (LENGTH(t.sentence) - LENGTH(REPLACE(t.sentence, ' ', '')))
ORDER BY value

) AS x
GROUP BY x.value

) AS y
GROUP BY value

这是完整的工作 fiddle :http://sqlfiddle.com/#!2/17481a/1

首先,我们执行查询以提取所有单词,如 here 所述@peterm(如果你想自定义处理的单词总数,请按照他的说明进行操作)。然后我们将其转换为子查询,然后我们COUNTGROUP BY 每个单词的值,然后在此之上进行另一个查询以GROUP BY 未对可能存在伴随符号的词进行分组。即:你好=你好!用 REPLACE

关于mysql - 计算表列中的单词出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1094888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com