MySQL关联表COUNT()和GROUP BY
我正在做一个非常正常的例行公事,但很难让我的输出正确。
我有两个表:*ads_list*(列表)和*ads_cate*(类别)。
我目前显示我的类别列表,如下所示:
SELECT id, cateName FROM ads_cate ORDER BY cateName
我想要实现的目标:每个类别中所有项目的计数,格式如下:
类别|广告数量类别
名称 56
这是我当前的代码,并且一直在调整,但在我的数组中没有输出:
SELECT
ads_cate.id,
ads_cate.cateName, // Category Name
ads_list.id,
ads_list.COUNT(title), // Title of ad
ads_list.Category // Relational Category ID INT(11)
FROM
ads_cate,
ads_list
GROUP BY cateName
ORDER BY cateName
我正在调用所有必填字段并在我的标题字段上运行 COUNT() (因为这些每个广告都是唯一的),然后我按 cateName 进行分组,这似乎也是正确的。
I am doing a pretty normal routine, but having a tough time getting my output correct.
I have two tables: *ads_list* (listings) and *ads_cate* (categories).
I am currently displaying my category list like so:
SELECT id, cateName FROM ads_cate ORDER BY cateName
What I am trying to achieve: count of all items in each category in this format:
Category | Number of Ads
categoryName 56
This is my current code, and have been tweaking but getting no output in my array:
SELECT
ads_cate.id,
ads_cate.cateName, // Category Name
ads_list.id,
ads_list.COUNT(title), // Title of ad
ads_list.Category // Relational Category ID INT(11)
FROM
ads_cate,
ads_list
GROUP BY cateName
ORDER BY cateName
I am calling in all required fields and running a COUNT() on my title field (as these are unique for each ad) and then I am grouping by cateName which also seems correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这会给你带来什么。我认为这就是你所需要的。
See what this gives you. I think it is what you need.