MySQL如何找到相对于父级的子行总数

编程入门 行业动态 更新时间:2024-10-28 19:20:28
本文介绍了MySQL如何找到相对于父级的子行总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一张桌子,上面有这样的亲子关系,

I have a table which having parent child relatiionship like this,

Employee_ID | Employee_Manager_ID | Employee_Name -------------------------------------------------------- 1 | 1 | AAAA 2 | 1 | BBBB 3 | 2 | CCCC 4 | 3 | DDDD 5 | 3 | EEEEE

是否可以通过单个查询获得所有雇员的总数(不只是直属子女,而是所有子子女的总数)?

Is it possible to get the count of all the employees come under a particular employee(Not only direct child,count of all the childs of child ) using a single query ?

Eg if the input = 1 output should be 4 if input = 2 ,output should be 3

预先感谢

推荐答案

假设您的表是:

mysql> SELECT * FROM Employee; +-----+------+-------------+------+ | SSN | Name | Designation | MSSN | +-----+------+-------------+------+ | 1 | A | OWNER | 1 | | 10 | G | WORKER | 5 | | 11 | D | WORKER | 5 | | 12 | E | WORKER | 5 | | 2 | B | BOSS | 1 | | 3 | F | BOSS | 1 | | 4 | C | BOSS | 2 | | 5 | H | BOSS | 2 | | 6 | L | WORKER | 2 | | 7 | I | BOSS | 2 | | 8 | K | WORKER | 3 | | 9 | J | WORKER | 7 | +-----+------+-------------+------+ 12 rows in set (0.00 sec)

查询是:

SELECT SUPERVISOR.name AS SuperVisor, GROUP_CONCAT(SUPERVISEE.name ORDER BY SUPERVISEE.name ) AS SuperVisee, COUNT(*) FROM Employee AS SUPERVISOR INNER JOIN Employee SUPERVISEE ON SUPERVISOR.SSN = SUPERVISEE.MSSN GROUP BY SuperVisor;

查询将产生如下结果:

+------------+------------+----------+ | SuperVisor | SuperVisee | COUNT(*) | +------------+------------+----------+ | A | A,B,F | 3 | | B | C,H,I,L | 4 | | F | K | 1 | | H | D,E,G | 3 | | I | J | 1 | +------------+------------+----------+ 5 rows in set (0.00 sec)

[答案]: 这对于一个级别(立即监督)可以找到您必须在while循环中使用的所有可能级别的所有监督(使用存储过程).

[Answer]: This for One level (immediate supervise) to find all supervises at all possible level you have to use while loop (use stored procedures).

尽管可以在每个级别上检索雇员然后采用他们的UNION,但是通常情况下,我们不能在不使用循环机制的情况下指定诸如在所有级别上检索雇员的受管理者"之类的查询. "

Although it is possible to retrieve employees at each level and then take their UNION, we cannot, in general, specify a query such as "retrieve the supervisees of a employee at all levels" without utilizing a looping mechanism."

参考:在此幻灯片中读取幻灯片编号23. 该书是关系代数和关系微积分"一章中的"FourthEdition数据库系统基础知识",主题为递归封闭运算".

REFERENCE: in this slide read slid number 23. The BOOK is " FUNDAMENTALS OF FourthEdition DATABASE SYSTEMS" in chapter "The Relational Algebra and Relational Calculus" there is a topic "Recursive Closure Operations".

添加查询以创建表,可能对您有帮助:

Adding Query for Table creation, May be helpful to you:

mysql> CREATE TABLE IF NOT EXISTS `Employee` ( -> `SSN` varchar(64) NOT NULL, -> `Name` varchar(64) DEFAULT NULL, -> `Designation` varchar(128) NOT NULL, -> `MSSN` varchar(64) NOT NULL, -> PRIMARY KEY (`SSN`), -> CONSTRAINT `FK_Manager_Employee` FOREIGN KEY (`MSSN`) REFERENCES Employee(SSN) -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1; Query OK, 0 rows affected (0.17 sec)

您可以像这样检查Table:

You can check Table like:

mysql> DESCRIBE Employee; +-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | SSN | varchar(64) | NO | PRI | NULL | | | Name | varchar(64) | YES | | NULL | | | Designation | varchar(128) | NO | | NULL | | | MSSN | varchar(64) | NO | MUL | NULL | | +-------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)

更多推荐

MySQL如何找到相对于父级的子行总数

本文发布于:2023-10-19 15:13:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507904.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相对于   总数   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!