[英]MySQL : GROUP_CONCAT the column and its count
I'm having a customer_status
table where the column "id" is unique and cust_id
is customer id and "status" is the current status of the customer. 我有一个
customer_status
表,其中的“ id”列是唯一的,而cust_id
是客户ID,而“ status”是客户的当前状态。
The table is as follows: 下表如下:
id cust_id status
1 1 NEW
2 2 NEW
3 1 VIEWED
4 1 NEW
5 1 VIEWED
6 1 NEW
Now my requirement is to show the status and the status count for each customer. 现在,我的要求是显示每个客户的状态和状态计数。
For example consider cust_id = 1 then output should be: 例如,考虑cust_id = 1,则输出应为:
NEW : 3
VIEWED : 2
I tried using group_concat but couldnot succeed with displaying status and status count, seperated with colon. 我尝试使用group_concat,但无法成功显示状态和状态计数(以冒号分隔)。
Please help me out. 请帮帮我。
我想不用group_concat就能得到想要的东西:
SELECT status, COUNT(status) FROM customer_status WHERE cust_id = 1 GROUP BY status
尝试这个:
select count(status),status from customer_status where cust_id=1 group by status
您可以使用CONCAT:
select CONCAT(status,':',count(status)) from customer_status as result where cust_id = 1 group by status;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.