添加修改删除后,使用一条SQL语句更新分类表中对应的用户总数count()

-----------------------------------------

select a.id ,ifnull(b.cnt,0) as cnt from bc_company as a left join (
	select company_id,count(company_id) as cnt from bc_user where status = 1 group by company_id
) as b on a.id = b. company_id


-------------------------------------------------------
update bc_company a,(
	select a.id ,ifnull(b.cnt,0) as cnt from bc_company as a left join (
		select company_id,count(company_id) as cnt from bc_user where status = 1 group by company_id
	) as b on a.id = b. company_id
)b 
set a.user_count=b.cnt where a.id=b.id;

-------------------------------------------------------

发表评论

邮箱地址不会被公开。 必填项已用*标注