简体   繁体   English

无法绑定多部分标识符的原因

[英]Reason for The multi-part identifier could not be bound

error - The multi-part identifier "Grant.EmpID" could not be bound. 错误 - 无法绑定多部分标识符“Grant.EmpID”。

query - 查询 -

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

Why does this error occur ? 为什么会出现此错误? Can't I call a person by real name and also by nickname ? 我不能用真名和昵称叫一个人吗?

You're aliasing [Grant] . 你是别名[Grant] In other words, you're stating that from here on out, [Grant] will be referred to as gr . 换句话说,你说从现在开始, [Grant]将被称为gr

Use the ALIAS in the GROUP BY clause, not the tableName. GROUP BY子句中使用ALIAS ,而不是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.

here's the SQL Order of Operation 这是SQL的操作顺序

  • FROM clause FROM子句
  • WHERE clause WHERE子句
  • GROUP BY clause GROUP BY子句
  • HAVING clause HAVING子句
  • SELECT clause SELECT子句
  • ORDER BY clause ORDER BY子句

No you can't because sql server is not a human. 不,你不能因为sql server不是人类。 imagine we have an Employee table which references itself 想象一下,我们有一个引用自己的Employee表

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

Mng and Emp are two instances of Employee Mng和Emp是Employee的两个实例

so if I select 所以,如果我选择

select * from Employee e, Employee

it will return all employee TWO times, because I am telling give me employees once under the name Employee once under the name e (alias) 它将返回所有员工两次,因为我告诉我雇员一次名为Employee ,名字e (别名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM