简体   繁体   中英

How to count the occurrence of specific words in a Mysql db row?

I'm quite new to MySQL. I require to count the number of occurrences of specific words in a MySQL table/columns.

I came across the following code through another post @Raging Bull, which only provides the count of one specific word.

SELECT name,(CHAR_LENGTH(description)-
    CHAR_LENGTH(REPLACE(description,' is ','')))/CHAR_LENGTH(' is ') AS 
TotalCount
FROM TableName
GROUP BY name

Could someone help me to align it for multiple words to be counted. For example, I want to count "is", "as", "was", "how".

MySQL is not designed for this sort of thing. However, you can just add the values together. I thing the correct method is like this;

select ( (length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' is ', '')) / length(' is ') +
         (length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' as ', '')) / length(' as ') +
         (length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' was ', '')) / length(' was ') +
         (length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' how ', '')) / length(' how ')
        )
from t ;

Note the use of spaces at the beginning and end to capture words at the beginning and end of the description. Also, this assumes that only spaces are used for separating words.

Try using LENGTH instead of CHAR_LENGTH and truncate spaces from String, eg:

SELECT name,
    ROUND (   
        (
            LENGTH(description)
            - LENGTH(REPLACE(description, "is", "")) 
        ) / LENGTH("is")        
    ) AS count
FROM TableName

update

To count multiple words, you can write simillar ROUND queries and add them together, eg:

SELECT name,
    SELECT(
        ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is")) +
        ROUND((LENGTH(description)- LENGTH(REPLACE(description, "This", "")) ) / LENGTH("This")) +
        ROUND((LENGTH(description)- LENGTH(REPLACE(description, "That", "")) ) / LENGTH("That")) 
        ) AS `count`
FROM TableName

update 2

Here's the query to get the counts as individual columns:

SELECT name,
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is")) AS 'is count',
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "this", "")) ) / LENGTH("this")) AS 'this count',
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "by", "")) ) / LENGTH("by")) AS 'by count'

FROM TableName

update 3

Below is the query to get aggregated counts for words:

SELECT 'is', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is"))) AS `count` FROM TableName

UNION

SELECT 'this', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "this", "")) ) / LENGTH("this"))) AS `count` FROM TableName

UNION

SELECT 'by', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "by", "")) ) / LENGTH("by"))) AS `count` FROM TableName

Here's the SQL Fiddle .

select count(adm_no)  from class_manager

尝试在其中adm_no是列名而class_manager是表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM