Ask us if you have any question about PHP, MySQL, Apache,
Linux or optimizing your website for quick answers!!!

Count number of words in a MySQL column

It is a common requirement to count number of words in a column of a MySQL database table. But MySQL does not have any built in function to count number of words as it usually has for number of purposes.

But you don't need to worry about it. It can be achieved with a simple trick e.g. you have a column name and want to count number of words in it. This query will return number of words separated with space in a column

SELECT SUM( LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)
FROM table

If you want to count number of words separated by some other string for example comma (,) or semicolon (;), simply replace space with your required character.

Comments (14)