Skip to content
Back to skills

Mysql 部门层级人数统计

ASecurity

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

  • 611 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 11, 2026
educationsqlexpress

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add ECNU-ICALK/AutoSkill --skill 'mysql-部门层级人数统计' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Mysql 部门层级人数统计?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Mysql 部门层级人数统计
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ecnu-icalk-mysql-b9b37e30/badge)](https://www.skillsdirectory.com/skills/ecnu-icalk-mysql-b9b37e30)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "dc7ccc8b-bedb-4c23-aca3-713f311eb80f"
name: "MySQL 部门层级人数统计"
description: "用于生成MySQL查询,统计部门人数时需处理层级关系,确保上级部门人数包含所有下级部门人数。"
version: "0.1.0"
tags:
  - "MySQL"
  - "SQL"
  - "层级结构"
  - "递归查询"
  - "数据统计"
triggers:
  - "mysql 统计部门人数 层级结构"
  - "高级部门统计包含低级部门人数"
  - "mysql 递归统计部门人数"
  - "部门树形结构人数汇总"
---

# 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 递归统计部门人数
- 部门树形结构人数汇总

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in education →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.