Count Number of Words in MySql, MySql Words Count

If you want to count number of words in a field or column, in MySql, then this could be the right place for you. As far as I know, there is no MySql function which returns the total number of words used in a field, so we have to use some techniques or tricks for getting the words count in MySql.

Number Of Words In A Field For Each Row

For example, if there is a table tbl_test, having a field total_words, then the following query will be helpful:

SELECT LENGTH(total_words) - LENGTH(REPLACE(total_words, ' ', ''))+1
FROM tbl_test;

This will return the number of words in each row, which we have in the field total_words.

Number Of Words In A Field For All Rows

If there’s a requirement to get the sum of all rows, like how many words are there in the rows of total_words field, then the following query will be helpful:

SELECT SUM(LENGTH(total_words) - LENGTH(REPLACE(total_words, ' ', ''))+1)
FROM tbl_test;

Share with:


5 comments

Leave a Reply

Login with:



Your email address will not be published. Required fields are marked *