hopeless_user :
I need to find all the object's that contain a specific keyword, together with the amount found. I already started with it, but don't know how to find the amount. I properly need to use COUNT but I can't seem to get it to work.
select object
from object o
where o.CTN like '%keyword%';
Another thing is, to only get the CTN that are shared by more than 3.
Sebastian Brosch :
You can try the following:
SELECT object, FLOOR(((CHAR_LENGTH(object) - CHAR_LENGTH(REPLACE(object, 'keyword', ''))) / CHAR_LENGTH('keyword'))) AS cnt_matches
FROM object o
WHERE o.object LIKE '%keyword%'
HAVING cnt_matches > 3
You can also create a function to reuse this expression:
DELIMITER //
CREATE FUNCTION GetStringCount(strValue VARCHAR(200), charValue VARCHAR(200))
RETURNS INTEGER DETERMINISTIC NO SQL
BEGIN
RETURN (CHAR_LENGTH(strValue) - CHAR_LENGTH(REPLACE(strValue, charValue, ''))) / CHAR_LENGTH(charValue);
END
So your query looks like this:
SELECT object, GetStringCount(object, 'keyword') AS cnt_matches
FROM object o
WHERE o.object LIKE '%keyword%'
HAVING cnt_matches > 3