如何计算每个类别的项目? [英] How to count items per category?

查看:83
本文介绍了如何计算每个类别的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对网站上的产品进行过滤. 像这样:

I want to make a filtering of products on a site. Something like this:

Department
- lassics (13,395)
- Literary (111,399)
- History (68,606)
...

Format
- HTML (3,637)
- PDF (8)
- Audio CD (443)
...

Language
- English (227,175)
- German (10,843)
- French (10,488)
...

如何计算每个类别的产品?对于每个类别,单独的SQL查询将太慢,因为产品和类别过多.我建议也不要选择缓存.

How to count products per category? A separate SQL-query for each category would be too slow because there are too many products and categories. I suggest caching is not an option too.

使用MySQL EXPLAIN查询也许有意义(尽管它并不总是提供足够的信息)?还是使用狮身人面像搜索引擎进行计数?...最好的方法是什么?谢谢.

Maybe it makes sense to use MySQL EXPLAIN queries (though it not always provide adequate information)? Or maybe using sphinx search engine for counting?... What is the best way to do this? Thanks.

推荐答案

尝试:

SELECT category, COUNT(*) as count FROM table GROUP BY category

响应应该是所有不同的category值,以及每个值的出现次数.

The response should be all the different category values, and the number of occurrences of each.

这篇关于如何计算每个类别的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆