mysql如何查找相对于父级的子行总数

时间:2012-12-06 05:56:50

标签: mysql select recursive-query

我有一张像这样有父子关系的桌子,

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

是否可以使用单个查询将所有员工的人数归入特定员工(不仅是直接子女,孩子的所有孩子的数量)?

Eg if the input  = 1
output should be 4

if input  = 2 ,output should be  3

提前致谢

2 个答案:

答案 0 :(得分:2)

假设您的表格为:

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)

[<强>答案]: 这对于一个级别(立即监督)来查找所有可能级别的所有监督,您必须在循环时使用(使用存储过程)。

虽然可以检索每个级别的员工然后采用他们的UNION,但我们通常不能指定查询,例如“在所有级别检索员工的受监管者”而不使用循环机制。

REFERENCE:在此幻灯片中读取滑动的数字23。 本书是“关系代数和关系微积分”一章中的“第四版数据库系统的基础”,主题是“递归闭包操作”。


为表创建添加查询可能对您有所帮助:

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)

您可以查看表格:

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)  

答案 1 :(得分:0)

你可以试试这个:

    SELECT
      table_name.Employee_ID,
      table_name.Employee_Name,
      COUNT(*) AS children
    FROM
      table_name AS t_one
    INNER JOIN table_name AS t_two ON
      t_two.Employee_Manager_ID=t_one.Employee_ID
    GROUP BY
      t_one.Employee_ID