[英]Join two tables with Mapping and get result
I'm use HBase DB with phoenix, I have 3 tables in which two are Main table which have many to many relation ship between them which is mapped by the third table. 我将HBase DB与phoenix一起使用,我有3个表,其中两个是Main表,它们之间有多对多的关系,它们由第三个表映射。
Employee 雇员
EmpID EmpName
1 Robert
2 John
3 Sansa
4 Ned
5 Tyrion
6 George
7 Daenerys
8 Arya
9 Cersie
10 Catelyn
Department 部
DepID DepName
1 Hardware
2 Software
3 Admin
4 HR
Department_Employee_Mapping Department_Employee_Mapping
ID DepID EmpID
1 1 2
2 1 6
3 2 1
4 3 5
5 3 6
6 4 3
7 4 7
8 4 10
9 4 5
I want to get names and department of all the employees who are in Admin Department, but also I want the details of those employee who belong to other department and who do not belong to any of the departments and these data should appear as NULL in the resultset Only the value for admin department should appear and if the employee belong to multiple departments, the result set should contain the value the Admin department and the other one will be ignored, Result set will look like 我想获取管理部门中所有员工的姓名和部门,但是我也想要那些属于其他部门但不属于任何部门的员工的详细信息,这些数据应在部门中显示为NULL。结果集仅显示管理部门的值,并且如果雇员属于多个部门,则结果集应包含管理部门的值,而另一个将被忽略,结果集将如下所示
Emp Name Dep Name
Robert NULL
John NULL
Sansa NULL
Ned NULL
Tyrion Admin
George Admin
Daenerys NULL
Arya NULL
Cersie NULL
Catelyn Admin
Use 2 LEFT JOIN and include the filtering on 'Admin' in the join against Department 使用2 LEFT JOIN并在针对Department的联接中包括对“ Admin”的过滤
SELECT DISTINCT e.empname, d.depname
FROM Employee e
LEFT JOIN Department_Employee_Mapping x ON x.empid = e.id
LEFT JOIN Departmentd ON d.id = x.depid AND d.id = 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.