简体   繁体   English

mysql计算字段中特殊字符的出现次数

[英]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.我想计算一个字段中所有出现的@符号,最初我认为LIKE '%@%'将是要走的路,但如果该字符多次出现在该字段中,它只会将其视为一个。

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.您可以通过使用 php.ini 中的 ``substr_count 函数使这更加简单。 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.这将返回的是数据库中“主题”字段中 @ 出现的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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