简体   繁体   English

SQL:计算每个类别中的项目

[英]SQL: Count items in each category

SELECT items.id, 
       items.category, 
       COUNT(*) 
  FROM items 
 GROUP BY items.id, 
          items.category 

在此处输入图片说明
I want to display how many items in each category.我想显示每个类别中有多少项目。 For example,例如,
category 1 - 6类别 1 - 6
category 2 - 7类别 2 - 7
category 3 - 4 ...类别 3 - 4 ...
Please help me!请帮我! I try this request and show me all the items with category :/我尝试此请求并显示所有类别为:/

Try this...尝试这个...

SELECT items.category, 
       COUNT(*) AS Count 
FROM   items 
GROUP  BY items.category 

If, you want to display count based of category then use group by clause with colum category如果,您想显示基于类别的计数,则使用带有列category group by子句

SELECT category, count(*) as Noofitems
FROM items i
GROUP BY category; 

Tiny word of advice :- Use table alise that could be easy to follow and read/wrire小小的建议:-使用易于理解和阅读/编写的表格别名

尝试这个...

select items.category,COUNT(items.COUNT(*)) from items Group By items.category;

Please use 'distinct'请使用“不同”

SELECT distinct items.category, 
       COUNT(*) AS Count 
FROM   items 
GROUP  BY items.category

to get the correct count for each unique category获得每个唯一类别的正确计数

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

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