4

错误 - 无法绑定多部分标识符“Grant.EmpID”。

询问 -

select emp.EmpID, 
COUNT(*) as CountRecords,
COUNT(GrantName) AS CountValues
From Employee as emp full join [Grant] as gr
on emp.EmpID = [Grant].EmpID 
-- This is the cause for the error ! Change it to gr.EmpID
group by emp.EmpID

为什么会出现这个错误?我不能用真名和昵称称呼一个人吗?

4

2 回答 2

6

你在别名[Grant]。换句话说,您是说从现在开始,[Grant]将被称为gr.

使用ALIASinGROUP BY子句,而不是 tableName。

SELECT emp.EmpID, 
       COUNT(*) as CountRecords,
       COUNT(GrantName) AS CountValues
FROM   Employee as emp 
       FULL JOIN [Grant] as gr
          on emp.EmpID = gr.EmpID  -- use the alias.
GROUP BY gr.EmpID                  -- use the alias.

这是 SQL 操作顺序

  • FROM 子句
  • WHERE 子句
  • GROUP BY 子句
  • HAVING 子句
  • 选择子句
  • ORDER BY 子句
于 2012-12-22T05:29:21.333 回答
0

不,你不能,因为 sql server 不是人类。假设我们有一个引用自身的 Employee 表

select * 
from Employee Emp, Employee Mng
where Emp.ManagerID = Mng.EmployeeID

Mng 和 Emp 是 Employee 的两个实例

所以如果我选择

select * from Employee e, Employee

它将返回所有员工两次,因为我告诉我给我员工一次以该名义Employee一次以该名义e(别名)

于 2012-12-22T05:41:07.947 回答