[英]How to perform UNION of the tables with same column name as their primary keys but different values
These are the two tables: Input:这是两张表: 输入:
Employees table:
+-------------+----------+
| employee_id | name |
+-------------+----------+
| 2 | Crew |
| 4 | Haven |
| 5 | Kristian |
+-------------+----------+
Salaries table:
+-------------+--------+
| employee_id | salary |
+-------------+--------+
| 5 | 76071 |
| 1 | 22517 |
| 4 | 63539 |
+-------------+--------+
I want an output like how you perform join but using the union.我想要一个输出,比如你如何执行加入但使用联合。
The output of the union should look like this:联合的输出应如下所示:
employee_id | name | salary
2 crew. null
4. haven. 63539
5. Kristian 76071
1. null. 22517
and post doing the union I want to perform select, by selecting the employee_id on employees with either no name or no salary并发布我想要执行选择的工会,方法是选择没有姓名或没有薪水的员工的employee_id
Query currently I am working on :目前我正在处理的查询:
select * from
(select employee_id, name, null as salary from employees
union all
select employee_id, null as name, salary from salaries)
as emp
where name is null or salary is null
The result looks like this:结果如下所示:
{"headers": ["employee_id", "name", "salary"], "values": [[2, "Crew", null], [4, "Haven", null], [5, "Kristian", null], [5, null, 76071], [1, null, 22517], [4, null, 63539]]} {"headers": ["employee_id", "name", "salary"], "values": [[2, "Crew", null], [4, "Haven", null], [5, "Kristian" , null], [5, null, 76071], [1, null, 22517], [4, null, 63539]]}
if use this query:如果使用此查询:
select * from (select employee_id, name from employees union all select employee_id, salary from salaries) as emp select * from (selectemployee_id, name from employees union all selectemployee_id,salary from paid) as emp
the result looks like this:结果如下所示:
{"headers": ["employee_id", "name"], "values": [[2, "Crew"], [4, "Haven"], [5, "Kristian"], [5, "76071"], [1, "22517"], [4, "63539"]]} {"headers": ["employee_id", "name"], "values": [[2, "Crew"], [4, "Haven"], [5, "Kristian"], [5, "76071" ], [1, "22517"], [4, "63539"]]}
This is a full join:这是一个完整的联接:
Select coalesce(e.employeeid, s.employeeid) employed
e.name, e.salary
From employees e full join
Salaries s
On e.employeeid = s.employeeid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.