Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql count occurrences of special character in a field

Tags:

php

mysql

I am wanting to count all occurrences of the @ symbol in a field and originally i thought LIKE '%@%' would be the way to go, but if the character appears in the field more than once it only counts it as one.

What other method are there that i could use that would count every occurrence?

Thanks.

EDIT For anyone needing it, this is what i ended up using that works.

$count = 0;
$sql = mysql_query("SELECT LENGTH(field_name) - LENGTH(REPLACE(field_name,'@','')) AS 'occurs' FROM table_name WHERE field_name LIKE '%@%'");
while ($data = mysql_fetch_assoc($sql)) {
    $count += $data['occurs'];
}
echo $count;
like image 446
FoxyFish Avatar asked Jun 04 '13 16:06

FoxyFish


1 Answers

select length('aa:bb:cc:dd')-length(replace('aa:bb:cc:dd',':',''));

source: http://lists.mysql.com/mysql/215049

like image 125
WouterH Avatar answered Sep 22 '22 22:09

WouterH