
mysql高效查询部门及其子部门员工
问题:如何高效地查询某个部门及其所有子部门下的员工,避免重复数据?
表结构:
查询方案:
WITH RECURSIVE depts(id)
AS(
SELECT id FROM department WHERE id = 要查找的部门ID
UNION ALL
SELECT id FROM department AS d where d.parent_id = id
)
select * from user where user.id in (
SELECT user_id FROM department_user_relate
where dept_id in (
select id from depts )
)扩展:
以上就是如何高效查询某个部门及其所有子部门的员工?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号