有一张分类表,4个字段,id,name,parent_id,child_num,其中parent_id是该分类的父分类id,现在需要更新所有分类的child_num,即子分类个数。
UPDATE category AS b SET child_num = ( SELECT count( * ) FROM ( SELECT * FROM category ) AS x WHERE x.parent_id = b.id )
ACTom's Blog
有一张分类表,4个字段,id,name,parent_id,child_num,其中parent_id是该分类的父分类id,现在需要更新所有分类的child_num,即子分类个数。
UPDATE category AS b SET child_num = ( SELECT count( * ) FROM ( SELECT * FROM category ) AS x WHERE x.parent_id = b.id )