繁体   English   中英

检索列mySQL PHP的计数

[英]Retrieving count of column mySQL PHP

大家好,我有一个带有4列的名为兴趣的mysql表。 interestID,名称,categoryID interest_desc和日期。 categoryID列链接到单独的表。 我将如何使用mysql查询来检查某个类别中有多少兴趣?

我猜我使用某种count()查询吗?

多谢你们

更新-

$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;

越来越近,但仍不完美,我想回显出具有最多interestID的categoryID

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;

计算+分组依据,
假设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

从兴趣中选择COUNT(interestID),其中categoryID ='yourvalue';

SELECT COUNT(interestID),categoryID来自兴趣GROUP BY categoryID;

由于您正在使用插入查询,因此每个查询都会插入一条记录,因此只需使用计数器变量计算您运行的插入查询的数量。

暂无
暂无

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

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