MySQL 统计总数和各分组数量

需求1:统计出用户表中的总数和各个分组内的用户数量

SELECT
	COALESCE (`group_id`, '总数'),
	COUNT(`id`) '客户数'
FROM
	crm_user
GROUP BY
	group_id WITH ROLLUP

效果

 MySQL 统计总数和各分组数量_第1张图片

需求2 :统计出跟进表客户跟进次数,并更新到客户表中的跟进次数字段

-- 根据跟进表跟进次数以及最后跟进时间,更新uid为111用户客户表中的客户跟进次数
UPDATE crm_customer sc,crm_share_join s,
(SELECT
	cid,
	now_time,
	COALESCE (`cid`, '总数'),
	COUNT(`id`) num
FROM
	crm_follw
WHERE uid = 111
GROUP BY
	cid WITH ROLLUP) as a 
SET sc.follow_up = a.num,sc.follw_time = a.now_time 
WHERE s.cid = a.cid  AND s.beshare_uid = 111 AND sc.id = s.bid AND sc.follow_up = 0

 

你可能感兴趣的:(MySQL)