为什么Apache Drill选择具有不同表的相同名称的列

时间:2017-08-17 12:20:55

标签: apache-drill

我有一个链接到Drill实例的PostgreSQL数据库。

每当我尝试加入两个都有name列的表时,每当我想选择此name时,Drill会选择错误的name列。我做错了什么?

鉴于以下两个表:

Department
| id | name |
|----|------|
| 1  |  A   |
| 2  |  B   |

Employee
| id | name | dept | salary |
|----|------|------|--------|
| 1  |  U   |  1   |  100   |
| 2  |  V   |  1   |   75   |
| 3  |  W   |  1   |  120   |
| 4  |  X   |  2   |   95   |
| 5  |  Y   |  2   |  140   |
| 6  |  Z   |  2   |   55   |

运行

select employee.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'

返回

| name | salary |
|------|--------|
|  A   |  100   |
|  A   |  75    |
|  A   |  120   |

运行

select dept.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'

返回

| name | salary |
|------|--------|
| null |  100   |
| null |  75    |
| null |  120   |

对我来说,有效但似乎非常愚蠢的是:

select dept.name, employee.salary
from employee
inner join (select id, name as deptname from department) as department on employee.dept = department.id
where department.deptname = 'A'

1 个答案:

答案 0 :(得分:0)

这似乎是因为你使用

select dept.name, [...]

但您从未为表departmentdepartment AS dept)分配别名。因此

select department.name, [...]

应该产生您正在寻找的价值。