简体   繁体   English

如何对列名与其主键相同但值不同的表执行 UNION

[英]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.

相关问题 如何将具有不同值但名称相同且使用datediff的主键分组 - How group primary keys with different value but same name and using datediff 如何在2个不同的表中强制使用不同的主键? - How to enforce different primary keys in 2 different tables? 合并两个表后如何按两个表中具有相同列名的列排序? - How to order by the column which has the same column name in two tables after union two tables? mySQL:如何遍历3个不同的表,引用每个表中的不同列,但使用主键 - mySQL: how to go along 3 different tables, referring to different column in each table but using primary keys 如何从SQL中的两个不同表中获取相同列名的值 - how to get values for same column name from two different tables in SQL 乘以主键链接的不同表中的值 - Multiplying values from different tables linked by primary keys SQL:如何连接具有相同含义但列名不同的表? - SQL: how to join tables with same meaning but different column name? 联合5个或更多具有相同结构和主键的表 - Union 5 or more Tables with the same structure and primary key 如何比较来自不同表的值具有相同名称的mysql - how to compare values from different tables with same name mysql 具有相同列名但ID不同的2个表的视图 - view of 2 tables with same column name but ids are different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM