组合来自不同表的列值
Combining column values from different tables
我卡住了。我有 2 张桌子 - 看图 1 Table columns And i would like to build query, that will give me the result - it is showed on image no 2. the result of query。
我有 2 个查询,我想将它们混合起来,以从图像 2 中获取列表。请帮助我,如何构建查询。
- 查询 1:SELECT department_name,department_id 来自 department_id 在 90 和 110 之间的部门;
- 查询 2:SELECT last_name,department_id 来自雇员 WHERE department_id 90 AND 110 之间;
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
我的示例输出 table
+-------+----+------------+
| KING | 10 | ACCOUNTING |
| BLAKE | 30 | SALES |
| CLARK | 10 | ACCOUNTING |
| JONES | 20 | RESEARCH |
| SCOTT | 20 | RESEARCH |
+-------+----+------------+
查询:
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
您不需要 "with ..." 部分;只需在两个因式子查询之后("input data" 之后)使用从 SELECT 语句开始的查询。我什至在每个部门内为您按姓氏排序;如果不需要,只需从 ORDER BY 子句中删除 "c_name"。
我调用了第一列c_name;您可以随意称呼它,但是当它还包含员工姓氏时称它为 department_name 对我来说意义不大。要随意调用它,请将 SELECT 语句从 SELECT c_name, department_id
更改为 SELECT c_name AS whatever, department_id...
我卡住了。我有 2 张桌子 - 看图 1 Table columns And i would like to build query, that will give me the result - it is showed on image no 2. the result of query。
我有 2 个查询,我想将它们混合起来,以从图像 2 中获取列表。请帮助我,如何构建查询。
- 查询 1:SELECT department_name,department_id 来自 department_id 在 90 和 110 之间的部门;
- 查询 2:SELECT last_name,department_id 来自雇员 WHERE department_id 90 AND 110 之间;
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
我的示例输出 table
+-------+----+------------+
| KING | 10 | ACCOUNTING |
| BLAKE | 30 | SALES |
| CLARK | 10 | ACCOUNTING |
| JONES | 20 | RESEARCH |
| SCOTT | 20 | RESEARCH |
+-------+----+------------+
查询:
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
您不需要 "with ..." 部分;只需在两个因式子查询之后("input data" 之后)使用从 SELECT 语句开始的查询。我什至在每个部门内为您按姓氏排序;如果不需要,只需从 ORDER BY 子句中删除 "c_name"。
我调用了第一列c_name;您可以随意称呼它,但是当它还包含员工姓氏时称它为 department_name 对我来说意义不大。要随意调用它,请将 SELECT 语句从 SELECT c_name, department_id
更改为 SELECT c_name AS whatever, department_id...