Oracle SQL - 基于外键查询 2 个表
问题描述:
我有 2 个表,我想进行单个查询并根据它们的外键对它们进行分组.这是一个例子:
I have 2 tables and i want to make a single query and group them based on their foreign key. Here's an example :
部门
DEPART_ID(PK) - DEPART_NAME
1 - Accounting
2 - IT
员工
EMP_ID(PK) - NAME - SURNAME - DEPART_ID(FK)
1 - John - John - 1
2 - George - George - 2
经理
MANAG_ID(PK) - NAME - SURNAME - DEPART_ID(FK)
1 - Nick - Nick - 1
2 - Michael - Michael - 2
而且我想将这个输出按部门名称分组.
And i want to get this output grouped by their department name.
Accounting | IT
----------------|-------------------
John John | George George
Nick Nick | Michael Michael
我试过了,但我不能让它工作.最好的方法是什么?
I've tried but i can't make it to work. Whats the best way to do this?
答
你可以使用这个:
select * from
(select name || ' ' || Surname Accounting from employees where depart_id = 1),
(select name || ' ' || Surname IT from employees where depart_id =2)
union
select * from
(select name || ' ' || Surname Accounting from managers where depart_id = 1),
(select name || ' ' || Surname IT from managers where depart_id =2)