[英]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.请注意,您应该使用
mysqli
或PDO
而不是原始的 MySQL API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.