简体   繁体   中英

order count of Customer_id by posts for category no

I have mysql table with Customer id's and post by the customer for various categories.I would like to find out for each category the count of customer post in descending order.

Customer id     Category no
1                 1
2                 1
3                 1
1                 1
1                 2
1                 1
2                 1

Basically in the table above customer id 1 has highest post for 1 then 2 then 3. So what mysql query should I use to get the solution .

I really appreciate any help .Thanks in Advance.

SELECT customer_id, COUNT(*) FROM category GROUP BY customer_id, category_no

按2列分组

This will give you number of post entries in categories table grouped by categories and customers and ordered by their count in desending order:

select customer_id, category_no, count(1) as total_post
from category  
group by customer_id, category_no 
order by count(1) desc;

To get the highest contributing customer for each category, you can do this:

select category_no, max(cnt) as maxcnt,
       substring_index(group_concat(customer_id order by cnt desc)) as cust3
from (select category_no, customer_id, count(*) as cnt
      from category
      group by category_no, customer_id
     ) c
group by category_no;

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