检索 mySQL PHP 列的计数

发布于 2024-12-21 11:34:22 字数 430 浏览 1 评论 0原文

大家好,我有一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别低头,皇冠会掉 2024-12-28 11:34:22
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;
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;
累赘 2024-12-28 11:34:22

计数 + 分组依据,
假设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 + group by,
assuming interestID is the unique primary key,
and each interest is tied to single category (as what you have shown)

select categoryID, count(*) as total
from interests
group by categoryID;

// the above example is a simple group by ID without using inner join

output :-

categoryID, total
浸婚纱 2024-12-28 11:34:22

从兴趣中选择 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;

东走西顾 2024-12-28 11:34:22

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文