Oracle SQL - 基于外键查询 2 个表 [英] Oracle SQL - Query 2 tables based on their foreign keys
本文介绍了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)
这篇关于Oracle SQL - 基于外键查询 2 个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文