gpt4 book ai didi

php - mysql查询计算整行中的空格或单词

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

在我的数据库中有一个表,名称为 tableA。表的结构在这里给出

id      from     message
1 23 hi how are you
2 65 hey whats going on
3 74 enjoying the vacation with family
4 23 here in Australia its chilly season
5 74 hey sam whats the plan for tomorrow

看到 id 是 auto-increment.from 列表示哪个用户正在发送消息,消息列由 messages 组成。我想知道哪些用户发送了多少单词。所以我必须首先计算行中的单词然后我必须搜索整个列并找到重复的位置并为该特定用户添加单词。在我的示例中你可以看到来自 23 和 74 的两个位置即将到来。我希望我的输出为

    from       nos
23 10
65 4
74 12

在这里你可以看到 23 排成两行,所以我添加了两行的单词数,这样 74 也排成两行。请指导

最佳答案

你可以通过这个查询来统计单词数-

Select length('string')-length(replace('string',' ',''))+1;

对于你的表,你可以这样做

Select *,(length(message)-length(replace(message,' ',''))+1) from table_name

这将显示前面每个消息的字数。

为了你想要的结果,试试这个-

Select `from`, sum((length(message)-length(replace(message,' ',''))+1)) as nos from table_name group by `from`

关于php - mysql查询计算整行中的空格或单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10175012/

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