Mr.Happy Mr.Happy - 5 months ago 11
PHP Question

MySQL - How to get counts of specific value from all the records

I have course table and I am storing completion status of users with 'A'. Now I want to get how many

A
is available from
CompletionStatus
field for all records.

I want this result:
A
= 5.

Course Table:



CourseRecordIdx User CompletionStatus
--------------- ---- --------------------
1 152 A___A_______________
2 147 AA_______A__________


I have tried with
char_length
but getting count with underscore and I want to get only total of
A
:

SELECT char_length(CompletionStatus) FROM `course` where CourseRecordIdx = 36


Any idea how to get result with select query?

Thanks.

Answer

You can try with this simplest way:

 SELECT length(REPLACE("field_name","_","")) FROM `tbl_name`;