简体   繁体   English

计算每个类别的项目数sql php

[英]count items for each category sql php

using sql and php. 使用sql和php。

I have the following table schema 我有以下表格架构

id | item_id | category_id

I've sample data in this table 我在此表中有示例数据

id  | item_id | category_id

1   | 1525       | shoes
2   | 2565       | shoes
3   | 2558       | shirts
4   | 2885       | shirts
5   | 3545       | shirts
6   | 1885       | belts

Requirement: I need to count how many items are in each category, and echo the count and item_ids 要求:我需要计算每个类别中有多少个项目,并回显count和item_ids

for example "category shoes has 2 items - 1525,2565" I need to do this for all categories. 例如“类别鞋有2个项目-1525,2565”,我需要针对所有类别执行此操作。

SELECT category_id, COUNT(*) as num_items, GROUP_CONCAT(item_id) AS items
FROM YourTable
GROUP BY category_id

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

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