MySQL doesn't have any built in function to count number of occurrences of word in a string, so we need bit of tweak in our query to make this work.

In short, this is what following query will do:
occurances = total length of the given word in the string / length of given word


This example will count no of occurances of "mysql" in a row:

SELECT descrp, ROUND ( ( LENGTH(descp) - LENGTH( REPLACE ( descp, "mysql", "") ) ) / LENGTH("mysql") ) AS count   FROM table WHERE id=1;

Result will look like this:


Comments (0)
Leave a Comment

loader Posting your comment...