The query below returns all of different subscriptions a user has. In another column, I would like to return the count of how many times a given subscription occurs. Any advice on how do this?
I tried including
COUNT(GROUP_CONCAT(subscription_plan_id))
but it doesn't work.
SELECT GROUP_CONCAT(subscription_plan_id)
FROM
subscriptions
WHERE
created_at BETWEEN '2014-01-01' AND '2014-01-31'
GROUP BY
user_id
HAVING
COUNT(subscription_plan_id) > 1
Desired output:
group_concat...count
1,2,3...2
2,3...5
Just put the count()
in the select
:
SELECT GROUP_CONCAT(subscription_plan_id), COUNT(subscription_plan_id) as NumSubs
FROM subscriptions
WHERE created_at BETWEEN '2014-01-01' AND '2014-01-31'
GROUP BY user_id
HAVING COUNT(subscription_plan_id) > 1;
Add count
And Group by subscription_plan_id
SELECT GROUP_CONCAT(subscription_plan_id), COUNT(subscription_plan_id) as NumSubs FROM subscriptions WHERE created_at BETWEEN '2014-01-01' AND '2014-01-31' GROUP BY subscription_plan_id HAVING COUNT(subscription_plan_id) > 1;
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.