It is no-brainer that Normalizing your database is the best solution here.
Also, as the other answers suggested, achieving the mentioned task programmaticaly will be a simple and efficient solution, too.
But if the above 2 options are not suitable for you at all, then following is the way to achieve it with PL\SQL.
1) If you only want to find the Number of occurrence of a single word at a time, you can try following way:
select
distinct
'aaa' as Word,
ROUND (
(
CHAR_LENGTH(@str)
- CHAR_LENGTH( REPLACE (@str, 'aaa', "") ) //you can put the word here you want
) / CHAR_LENGTH('aaa')
) AS count
from t
;
Result:
word | count
--------------
aaa | 3
2) If you want to display the count of all the words as given in question, you need a Function, a Procedure and a Temp Table.
Step 1: Create an Temporary table.
create table temp(str varchar(10));
Step 2: Create a Function to split data with delimiter ,
with given position.
CREATE FUNCTION SPLIT_STR
(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
Now, the above function will split data only for the given position. In order to split whole string, we need to iterate this function. That's our step 3.
Step 3: Create Procedure to iterate the above function;
CREATE PROCEDURE ABC(IN fullstr varchar(1000))
BEGIN
DECLARE a INT Default 0;
DECLARE str VARCHAR(255);
simple_loop: LOOP
SET a=a+1;
SET str=SPLIT_STR(fullstr,",",a);
IF str='' THEN
LEAVE simple_loop;
END IF;
#Do Inserts into temp table here with str going into the row
insert into t1 values (str);
END LOOP simple_loop;
Step 4: Store whole column into one variable:
select @str:= group_concat(columnname separator ',') from t;
Step 5: Call Procedure:
call abc(@str);
Step 6: Get count from temp
table:
select
str as word,
count(*) as occurence
from temp
group by str;
Results:
word | occurence
-------------------------
1 aaa | 3
2 bbb | 1
3 ccc | 2
4 ddd | 2
5 eee | 1
Click here for the DEMO
Hope it helps!