我们假设我的系统中有3个级别的用户,一个用于ordinary staffs
,第二个用于two supervisors
,最后一个用于president
是最高的。监管人员是普通员工在系统发送请求时的批准人,而总裁可以在发送与公司有关的请求时批准监管人员和普通员工的请求。
假设我有一个表employee_information_tbl
,而列job_pos_status
的值为5 = ordinary staff
,6= supervisors
和7 = president
。
id name job_pos_status approver_id
-----------------------------------------------------------------------------
001 John Peter 7 001
002 Anne Sy 6 001
003 Abigail Sam 6 001
004 Paul Top 5 002
005 Lenny Bots 5 003
006 Steve Max 5 002
007 Max Collins 5 003
008 Anne Dy 5 003
009 Maine Mendoza 5 002
问题:
如何在不使用all
查询的情况下选择所有记录(总统可以访问),但是遵循其组织层次结构的逻辑记录?
答案 0 :(得分:0)
You want the query based on hierarchy, please check this :
create table employee_information_tbl (id nvarchar(3), name nvarchar(50), job_pos_status int, approver_id nvarchar(3) )
insert into employee_information_tbl
select '001', 'John Peter', 7, '001'
union all
select '002', 'Anne Sy', 6, '001'
union all
select '003', 'Abigail Sam', 6, '001'
union all
select '004', 'Paul Top', 5, '002'
union all
select '005', 'Lenny Bots', 5, '003'
union all
select '006', 'Steve Max', 5, '002'
union all
select '007', 'Max Collins', 5, '003'
union all
select '008',' Anne Dy', 5, '003'
union all
select '009', 'Maine Mendoza', 5, '002'
select a.id, a.name,
a.job_pos_status,
a.approver_id,
a.approver_id
from employee_information_tbl a
Inner join (select distinct job_pos_status
from employee_information_tbl
) b
on b.job_pos_status = a.job_pos_status
order by b.job_pos_status desc,
a.approver_id,
a.id
drop table employee_information_tbl