简体   繁体   中英

mysql count occurrences of special character in a field

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;
select length('aa:bb:cc:dd')-length(replace('aa:bb:cc:dd',':',''));

来源:http: //lists.mysql.com/mysql/215049

You could make this even simpler by using the ``substr_count function in php. see below.

$message = $row['themessage'];
echo substr_count($message, '@');

what this will return is the number of times @ has occurred in your "themessage" field in your database.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM