嘿,伙计们,我有一个mysql表叫兴趣与4列。interestID、名称、类别id、interest_desc和日期。categoryID列链接到一个单独的表。我如何使用mysql查询,检查有多少兴趣是在一个特定的类别?
我猜我使用某种计数()查询?
谢谢大家
Update -
$count_query_v1 = "SELECT categoryID, COUNT(*) AS total FROM interests GROUP by categoryID; "; $answer = mysql_query($count_query_v1) or die(mysql_error()); echo $answer;
越来越接近但仍然不完美,我想用最感兴趣的
select category_name, count(*) as total
from interests i left join category c on c.category_id = i.category_id
group by i.category_id;
count + group by,
假设interestID是唯一的主键,
每个兴趣都绑定到单个类别(如您所示)
select categoryID, count(*) as total
from interests
group by categoryID;
// the above example is a simple group by ID without using inner join
输出: -
categoryID, total
SELECT COUNT(interestID) FROM interests WHERE categoryID = 'yourvalue';
由于您正在使用插入查询,每个查询将插入一条记录,因此只需通过使用计数器变量来计算您运行的插入查询的数量。