我有一个EMPLOYEE表,其中有员工ID和员工名称映射
另一个表是TEAM_EMP_MAP,其中具有团队和员工映射
因此,根据TEAM_EMP_MAP表,团队ID为1111的团队拥有这些员工EMP_1,EMP_2和EMP_4。 我需要借助TEAM_EMP_MAP和EMPLOYEE表的输出如下。
我在Google上进行了很多检查,但没有成功。
答案 0 :(得分:0)
一种选择是在表联接的LIKE
子句中使用运算符ON
,如下所示:
SELECT t.ORG_ID TEAM_ID,
e.EMP_NM EMPLOYEE_NAME
FROM TEAM_EMP_MAP t INNER JOIN EMPLOYEE e
ON ',' || t.EMPLOYEES || ',' LIKE '%,' || e.EMP_ID || ',%'
见他demo。
结果:
> TEAM_ID | EMPLOYEE_NAME
> :------ | :------------
> 1111 | EMP_1
> 1111 | EMP_3
> 1111 | EMP_4
答案 1 :(得分:0)
好的,我知道,您需要使用正确的多对多关联来修复TEAM_EMP_MAP表。
在这种情况下,我认为您可以使用Regex(REGEXP_SUBSTR)来分隔逗号分隔的列。 看看这个post使其正常工作。
答案 2 :(得分:0)
一种方法是使用REGEXP_SUBSTR和CONNECT LEVEL BY在第二个表的数组中拆分内容。
让我给你看一个例子:
SQL> create table y ( id_emp number , name varchar2(2) ) ;
Table created.
SQL> insert into y values ( 101 , 'A' ) ;
1 row created.
SQL> insert into y values ( 104, 'B' ) ;
1 row created.
SQL> insert into y values ( 103 , 'C' ) ;
1 row created.
SQL> commit ;
Commit complete.
SQL> select * from t ;
ID VAL
---------- -------------
1111 101,104,103
SQL> select * from y ;
ID_EMP NAME
---------- ----
101 A
104 B
103 C
SQL> with emps as ( SELECT regexp_substr(val, '[^,]+', 1, LEVEL) as empid FROM t
2 CONNECT BY regexp_substr(val, '[^,]+', 1, LEVEL) IS NOT NULL )
3 select y.name , emps.empid from y inner join emps on ( y.id_emp = emps.empid ) ;
NAME EMPID
---- -----------------------------
A 101
B 104
C 103
答案 3 :(得分:0)
with t as (
select org_id
,employees txt
from team_emp_map
where org_id = 1111
)
,emp as ( -- split string into records
select org_id
,regexp_substr(t.txt, '\d+', 1, level) emp_id
from t
connect by regexp_substr(t.txt, '\d+', 1, level) is not null
)
select emp.org_id
,employee.emp_nm
from emp
,employee
where emp.emp_id = employee.emp_id