加入 Table 到另一个 Table 然后加入它自己
Join Table to Another Table then to Itself
我正在使用连接到 Oracle 9 的 PowerBuilder 12.5。我想 select 将我的员工 table 加入 employee_position table emp_nbr ,然后使用 emp_nbr 再次加入员工 table。
但是,我不想像下面那样使用 employee_copy table,因为它很快就会被删除。这是一张说明我想做什么的图片。 :
我不确定我是否应该使用嵌套 selects,或者这是否仅适用于内部连接。所以这个 SQL 代码有效,我成功检索了主管的姓名:
SELECT "EMPLOYEE"."EMP_NBR",
"EMPLOYEE"."DEPT_NBR",
"EMPLOYEE"."SHOP",
"EMPLOYEE"."LAST_NAME",
"EMPLOYEE"."FIRST_NAME",
"EMPLOYEE"."MIDDLE_INITIAL",
"EMPLOYEE"."EMP_CLASS",
"EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID",
"EMPLOYEE_COPY"."LAST_NAME",
"EMPLOYEE_COPY"."FIRST_NAME",
"EMPLOYEE_COPY"."MIDDLE_INITIAL"
FROM "EMPLOYEE",
"EMPLOYEE_POSITION",
"EMPLOYEE_COPY"
WHERE ( "EMPLOYEE"."EMP_NBR" = "EMPLOYEE_POSITION"."EMP_NBR" ) and
( "EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID" = "EMPLOYEE_COPY"."EMP_NBR" )
所以我的问题是:如何在不使用 employee_copy table 的情况下执行此操作?此外,这必须在一个 SQL 查询中完成。
没问题:自连接可以正常工作:
SELECT "EMPLOYEE"."EMP_NBR",
"EMPLOYEE"."DEPT_NBR",
"EMPLOYEE"."SHOP",
"EMPLOYEE"."LAST_NAME",
"EMPLOYEE"."FIRST_NAME",
"EMPLOYEE"."MIDDLE_INITIAL",
"EMPLOYEE"."EMP_CLASS",
"EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID",
"EMPLOYEE_MGR"."LAST_NAME" as mgr_last_name,
"EMPLOYEE_MGR"."FIRST_NAME" as mgr_first_name,
"EMPLOYEE_MGR"."MIDDLE_INITIAL" as mgr_last_name
FROM "EMPLOYEE",
"EMPLOYEE_POSITION",
"EMPLOYEE" EMPLOYEE_MGR
WHERE ( "EMPLOYEE"."EMP_NBR" = "EMPLOYEE_POSITION"."EMP_NBR" ) and
( "EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID" = "EMPLOYEE_MGR"."EMP_NBR" )
只需为 EMPLOYEE_MGR 的雇员 table 使用别名即可。
我正在使用连接到 Oracle 9 的 PowerBuilder 12.5。我想 select 将我的员工 table 加入 employee_position table emp_nbr ,然后使用 emp_nbr 再次加入员工 table。
但是,我不想像下面那样使用 employee_copy table,因为它很快就会被删除。这是一张说明我想做什么的图片。 :
我不确定我是否应该使用嵌套 selects,或者这是否仅适用于内部连接。所以这个 SQL 代码有效,我成功检索了主管的姓名:
SELECT "EMPLOYEE"."EMP_NBR",
"EMPLOYEE"."DEPT_NBR",
"EMPLOYEE"."SHOP",
"EMPLOYEE"."LAST_NAME",
"EMPLOYEE"."FIRST_NAME",
"EMPLOYEE"."MIDDLE_INITIAL",
"EMPLOYEE"."EMP_CLASS",
"EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID",
"EMPLOYEE_COPY"."LAST_NAME",
"EMPLOYEE_COPY"."FIRST_NAME",
"EMPLOYEE_COPY"."MIDDLE_INITIAL"
FROM "EMPLOYEE",
"EMPLOYEE_POSITION",
"EMPLOYEE_COPY"
WHERE ( "EMPLOYEE"."EMP_NBR" = "EMPLOYEE_POSITION"."EMP_NBR" ) and
( "EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID" = "EMPLOYEE_COPY"."EMP_NBR" )
所以我的问题是:如何在不使用 employee_copy table 的情况下执行此操作?此外,这必须在一个 SQL 查询中完成。
没问题:自连接可以正常工作:
SELECT "EMPLOYEE"."EMP_NBR",
"EMPLOYEE"."DEPT_NBR",
"EMPLOYEE"."SHOP",
"EMPLOYEE"."LAST_NAME",
"EMPLOYEE"."FIRST_NAME",
"EMPLOYEE"."MIDDLE_INITIAL",
"EMPLOYEE"."EMP_CLASS",
"EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID",
"EMPLOYEE_MGR"."LAST_NAME" as mgr_last_name,
"EMPLOYEE_MGR"."FIRST_NAME" as mgr_first_name,
"EMPLOYEE_MGR"."MIDDLE_INITIAL" as mgr_last_name
FROM "EMPLOYEE",
"EMPLOYEE_POSITION",
"EMPLOYEE" EMPLOYEE_MGR
WHERE ( "EMPLOYEE"."EMP_NBR" = "EMPLOYEE_POSITION"."EMP_NBR" ) and
( "EMPLOYEE_POSITION"."EMP_SUPERVISOR_ID" = "EMPLOYEE_MGR"."EMP_NBR" )
只需为 EMPLOYEE_MGR 的雇员 table 使用别名即可。