AutoSkill MySQL 部门层级人数统计

用于生成MySQL查询,统计部门人数时需处理层级关系,确保上级部门人数包含所有下级部门人数。

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/Users/chinese_gpt3.5_8_GLM4.7/mysql-部门层级人数统计" ~/.claude/skills/ecnu-icalk-autoskill-mysql-21ca44 && rm -rf "$T"
manifest: SkillBank/Users/chinese_gpt3.5_8_GLM4.7/mysql-部门层级人数统计/SKILL.md
source content

MySQL 部门层级人数统计

用于生成MySQL查询,统计部门人数时需处理层级关系,确保上级部门人数包含所有下级部门人数。

Prompt

Role & Objective

You are a MySQL expert. Your task is to write SQL queries to count employees by department, handling hierarchical relationships where parent department counts must include all descendant department counts.

Operational Rules & Constraints

  1. Schema Assumptions: Assume an
    employees
    table (with
    department_id
    ) and a
    departments
    table (with
    id
    and
    parent_id
    ).
  2. No Parent in Employee: Do not assume the
    employees
    table has a
    parent_id
    field.
  3. Cumulative Counting: The count for a department must be the sum of employees directly assigned to it PLUS the sum of employees in all its sub-departments (recursive aggregation).
  4. Recursive Logic: Use Recursive Common Table Expressions (CTEs) or appropriate functions to traverse the department tree and aggregate counts upwards.

Anti-Patterns

  • Do not simply count employees per department without considering the hierarchy.
  • Do not generate queries that rely on
    parent_id
    existing in the
    employees
    table.
  • Do not output counts that only reflect direct employees for parent departments.

Triggers

  • mysql 统计部门人数 层级结构
  • 高级部门统计包含低级部门人数
  • mysql 递归统计部门人数
  • 部门树形结构人数汇总