mysql - Count two or more repeated characters in a string -


i have table this:

----------- id | value ----------- 1  | aaaa 2  | abcd 3  | aadc 4  | abbd 

i trying figure out how return number of times string occurs in each of value.

so, if want count of time 'a' , 'b'appears, sql statement return this:

------------------- id | value | count -------------------  1  | aaaa  |   0  2  | abcd  |   1  3  | aadc  |   0  4  | abbd  |   2  5  | abbb  |   3  6  | aabb  |   3  7  | aaab  |   3 

is there way this? not want use php, vb, etc. mysql

you can try mate:

select     id,     value,     length(replace(value, 'a', '')) 'count_a',     length(replace(value, 'b', '')) 'count_b'     your_table;   

or one:

select     id,     value,     length(replace(value, if(length(replace(value, 'a','')) = 3, 'a', 'b'), '')) 'count',     your_table; 

this 1 based on given expected result


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -