0

I want to make a query in MySQL that counts the number of appearances of a character in specific field

for example:

Class          | ID 
============================
d              |1          
c;g;i;m        |2          
r;e            |3          

final resault should be:

Class          | ID        | NumOf; (to be added)
==========================================================
d              |1          | 0
c;g;i;m        |2          | 3
r;e            |3          | 1

thank you!

Abhik Chakraborty
  • 43,914
  • 5
  • 48
  • 61
  • 1
    possible duplicate of [Count the number of occurences of a string in a VARCHAR field?](http://stackoverflow.com/questions/12344795/count-the-number-of-occurences-of-a-string-in-a-varchar-field) – Jordan.J.D Jun 30 '14 at 12:50

1 Answers1

3

So that is with REPLACE():

SELECT CHAR_LENGTH(Class) - CHAR_LENGTH(REPLACE(Class, ';', '')) AS result FROM t

That is: count how many characters are before replacement and after.

Alma Do
  • 36,374
  • 9
  • 70
  • 101