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.
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.
You can use LENGTH
and REPLACE
for that purpose :
SELECT LENGTH(CompletionStatus) - LENGTH(REPLACE(CompletionStatus, 'A', '')) as count_Char
FROM `course`
This basically counts how many characters are in that string, and then checks the difference between that number, and the number of characters with out the specific character.
您可以尝试这种最简单的方法:
SELECT length(REPLACE("field_name","_","")) FROM `tbl_name`;
I suppose the following will work, first replace all _
with ''
. After that use char_length()
function.
SELECT CHAR_LENGTH(REPLACE(CompletionStatus,'_','') as totalA FROM course where CourseRecordIdx = 36;
You can write this query for this:
SELECT CourseRecordIdx,User,CompletionStatus,
ROUND (
(
LENGTH(CompletionStatus)- LENGTH( REPLACE ( CompletionStatus, "A", ""))
) / LENGTH("A")) AS count
from `course` where CourseRecordIdx = 36
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.