简体   繁体   English

获取mysql列字段中不同字符串值的计数?

[英]Get count of different values of string in mysql column fields?

在此处输入图片说明

when we select multiple values that stored in database as follows当我们选择存储在数据库中的多个值时在此处输入图片说明

now i want to count no of times values will be selected means from as above table现在我想计算从上表中选择值的次数

cloths 2 times
Electronics 2 times
Food 1 time

How it will possible ?这怎么可能?

Something like this might work:像这样的事情可能会奏效:

select c.cat, count(t.answer)
from (select 'cloths' as cat union all
      select 'electronics' union all
      select 'food'
     ) c left outer join
     table t
     on t.answer like concat('%', c.cat, '%')
group by c.cat;

Maybe something like this in PHP?也许在 PHP 中是这样的?

$options    = array('Cloths','Electronics', 'food');
$values     = array();
foreach($options as $option)
{
    $total = mysql_num_rows("SELECT * FROM TBL_SHOPPING WHERE answer '%".$option."%'");
    $values[$option] = $total;
}

Note that you should use mysqli or PDO instead of the original MySQL API.请注意,您应该使用mysqliPDO而不是原始的 MySQL API。

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

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