我卡住了。我有2个表 - 看看图像1 Table columns我想构建查询,它会给我结果 - 它显示在图像2号上。the result of query。
我有2个查询,我想将它们混合起来,从图像2中获取列表。请帮帮我,如何构建查询。
答案 0 :(得分:1)
<强>查询强>:
with departments (department_id, department_name) as (
select 90, 'Executive' from dual union all
select 100, 'Finance' from dual union all
select 110, 'Accounting' from dual
),
employees (employee_id, last_name, department_id) as (
select 1003, 'King' , 90 from dual union all
select 1005, 'De Hann' , 90 from dual union all
select 1009, 'Gietz' , 110 from dual union all
select 1013, 'Popp' , 100 from dual union all
select 1014, 'Chen' , 100 from dual union all
select 1015, 'Higgins' , 110 from dual union all
select 1029, 'Greenberg', 100 from dual union all
select 1040, 'Kochar' , 90 from dual union all
select 1043, 'Faviet' , 100 from dual union all
select 1045, 'Urman' , 100 from dual union all
select 1049, 'Sciarra' , 100 from dual
)
-- end input data; begin actual query --
select c_name, department_id from
( select department_name as c_name, department_id, 0 as categ from departments
union all
select ' ' || last_name as c_name, department_id, 1 from employees
order by department_id, categ, c_name
);
结果:
C_NAME DEPARTMENT_ID
------------- -------------
Executive 90
De Hann 90
King 90
Kochar 90
Finance 100
Chen 100
Faviet 100
Greenberg 100
Popp 100
Sciarra 100
Urman 100
Accounting 110
Gietz 110
Higgins 110
你不需要“带...”部分;只需在两个因子子查询之后(在“输入数据”之后)使用从SELECT语句开始的查询。我甚至为你排了各个部门的姓氏;如果不需要,只需从ORDER BY子句中删除“c_name”。
我调用了第一列c_name;你可以把它称为任何你想要的东西,但是当它也包含员工的姓氏时称它为department_name对我来说没有多大意义。要随意调用它,请将SELECT语句从SELECT c_name, department_id
更改为SELECT c_name AS whatever, department_id...
答案 1 :(得分:0)
SELECT c.last_name,
d.department_id,
d.department_name
FROM employee c
JOIN deptartment d ON d.department_id=c.department_id
WHERE d.department_id BETWEEN 90 AN 110
从我的示例表中输出
+-------+----+------------+
| KING | 10 | ACCOUNTING |
| BLAKE | 30 | SALES |
| CLARK | 10 | ACCOUNTING |
| JONES | 20 | RESEARCH |
| SCOTT | 20 | RESEARCH |
+-------+----+------------+