gpt4 book ai didi

mysql - 如何在 MySQL 中创建递归存储过程?

转载 作者:行者123 更新时间:2023-11-30 23:48:44 24 4
gpt4 key购买 nike

我目前正在使用 MySQL 数据库。我有一个表,它定义了员工角色。表架构如下所示:

|-------------------|
| headidx | subsidx |
|-------------------|
| 90001 | 90002 |
| 90001 | 90003 |
| 90002 | 90004 |
| 90002 | 90005 |
| 90002 | 90006 |
| 90007 | 90008 |
| 90001 | 90010 |
| 90010 | 90011 |
|-------------------|

head 列包含 head id,否则 subs 列包含 sub id。如果 head id 是“Diecrtor”,那么 subs 将是“manager”或“supervisor”,那么如果 subs id 是“Manager”,那么 subs id 就是“Staff”。如果我只有“director”id,我怎样才能获得 subs id 的成员?

插图:
输入:90001
结果:90002,90003,90004,90005,90006,90010,90011

输入:90002
结果:90004,90005,90006

输入:90010
结果:90011

请任何人帮助我如何解决这个问题。

最佳答案

你需要这个

SELECT subsidx 
FROM Table1
WHERE headidx >= 'YourValue'

Fiddle Demo

关于mysql - 如何在 MySQL 中创建递归存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com