简体   繁体   中英

count items for each category sql php

using sql and 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

for example "category shoes has 2 items - 1525,2565" I need to do this for all categories.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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