检索 mySQL PHP 列的计数
大家好,我有一个名为interests的mysql表,有4列。 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;
越来越接近但仍然不完美,我想用最感兴趣的 ID 来回显类别 ID
Hey guys I have a mysql table called interests with 4 columns. interestID, name, categoryID interest_desc and date. the categoryID column is linked to a seperate table. How would I use a mysql query that checked how many interests are in a certain category?
Im guessing i use some sort of count() query?
Thanks guys
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;
Getting closer but still not perfect, i want to echo out the categoryID with the most interestID's
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
计数 + 分组依据,
假设interestID是唯一主键,
每个兴趣都与单个类别(如您所显示的)
输出相关联:-
count + group by,
assuming interestID is the unique primary key,
and each interest is tied to single category (as what you have shown)
output :-
从兴趣中选择 COUNT(interestID),其中类别ID = 'yourvalue';
从兴趣组中选择 COUNT(interestID)、categoryID BY 类别 ID;
SELECT COUNT(interestID) FROM interests WHERE categoryID = 'yourvalue';
SELECT COUNT(interestID), categoryID FROM interests GROUP BY categoryID;
由于您使用的是插入查询,每个查询都会插入一条记录,因此只需使用计数器变量来计算您运行的插入查询的数量即可。
Since you are using the insert query each query will insert one record, so just count the number of insert queries you run by using a counter varialble.