danielhpavey.uk

Count number of words in text using MySQL

I was trying to count the number of words in some MySQL results today.

This is easy enough with PHP using str_word_count

However there is no simple command to do it in MySQL

However I found this great post that give a nifty little trick for counting words with MySQL:

SELECT SUM(LENGTH(name) – LENGTH(REPLACE(name, ‘ ‘, ”))+1)

FROM table

Nifty!

Date: Monday 10th September 2012