sql—如何使用mysql获取用户的顶级层次数据?

xiozqbni  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(296)

你好,
我的数据库表结构如下。

TableName:- UserTable

id | empName | reportsTo
-----------------------
1  | XYZ     | -
2  | ABC     | 1
3  | MNP     | 2
4  | IJK     | 3
5  | PQR     | 4
6  | DEF     | 3
7  | STU     | 2

我怎样才能在最高层中得到他向其报告的用户的详细信息。
当我选择id为的用户时 7 输出应该是

id    empName  reportsTo
2     ABC      1
1     XYZ      -

当我选择id为的用户时也是这样 6 它应该返回id的数据 3,2,1 .

8nuwlpux

8nuwlpux1#

我不认为你理解我发布的可能重复的链接,所以这里的解决方案发布了一些琐碎的调整,以适应您的数据。

drop table if exists usertable;
create table usertable(id int, empName varchar(3), reportsTo int);
insert into usertable values
(1  , 'XYZ'     , null),
(2  , 'ABC'     , 1),
(3  , 'MNP'     , 2),
(4  , 'IJK'     , 3),
(5  , 'PQR'     , 4),
(6  , 'DEF'     , 3),
(7  , 'STU'     , 2);

SELECT T2.id, T2.empname,t2.reportsto
FROM (
    SELECT
        @r AS _id,
        (SELECT @r := reportsto FROM usertable WHERE id = _id) AS parent_id,
        @l := @l + 1 AS lvl
    FROM
        (SELECT @r := 7, @l := 0) vars,
        usertable h
    WHERE @r <> 0) T1
JOIN usertable T2
ON T1._id = T2.id
where t2.id <> 7
ORDER BY T1.lvl;

+------+---------+-----------+
| id   | empname | reportsto |
+------+---------+-----------+
|    2 | ABC     |         1 |
|    1 | XYZ     |      NULL |
+------+---------+-----------+
2 rows in set (0.03 sec)

对于6号身份证

SELECT T2.id, T2.empname,t2.reportsto
FROM (
    SELECT
        @r AS _id,
        (SELECT @r := reportsto FROM usertable WHERE id = _id) AS parent_id,
        @l := @l + 1 AS lvl
    FROM
        (SELECT @r := 6, @l := 0) vars,
        usertable h
    WHERE @r <> 0) T1
JOIN usertable T2
ON T1._id = T2.id
where t2.id <> 6
ORDER BY T1.lvl; 

+------+---------+-----------+
| id   | empname | reportsto |
+------+---------+-----------+
|    3 | MNP     |         2 |
|    2 | ABC     |         1 |
|    1 | XYZ     |      NULL |
+------+---------+-----------+
3 rows in set (0.00 sec)

注意,对于id 1,我已将reports从-更改为null。只有当reports to小于id时,这种方法才有效。否则,您必须认真考虑一下您的数据结构。

ztmd8pv5

ztmd8pv52#

有一个临时的解决方案,但不好继续使用它。你必须放下你的table,创建一个新的有组织的table
第一次计数不同 reportsto 价值观

SELECT COUNT(DISTINCT reportsto) FROM test

你会得到一个号码。假设你得到(5),那么有5个是一些员工的老板

SELECT * FROM test WHERE 
id=(SELECT @boss := reportsto FROM test WHERE id=7) 
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)
OR 
id=(SELECT @boss := reportsto FROM test WHERE id=@boss1)
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)
OR 
id=(SELECT @boss := reportsto FROM test WHERE id=@boss1)

如果你得到(10),那么查询将是

SELECT * FROM test WHERE 
id=(SELECT @boss := reportsto FROM test WHERE id=7) 
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)
OR 
id=(SELECT @boss := reportsto FROM test WHERE id=@boss1)
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)
OR 
id=(SELECT @boss := reportsto FROM test WHERE id=@boss1)
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)
OR 
id=(SELECT @boss := reportsto FROM test WHERE id=@boss1)
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)
OR 
id=(SELECT @boss := reportsto FROM test WHERE id=@boss1)
OR 
id=(SELECT @boss1 := reportsto FROM test WHERE id=@boss)

.
.
.
等等

相关问题