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

Count number of times a word exists in a column or string

MySQL does not provide any native function to count the number of times a specific word exists in a column or string. But you can do it yourself by using MySQL string functions LENGTH() and REPLACE(). e.g. you have a column description and want to count number of instances of a word 'iPhone'. This query will return the number of occurrences of word iPhone.

SELECT (LENGTH(description) - LENGTH(REPLACE(description,'iPhone','')))  / LENGTH('iPhone')
FROM table

Comments