在SQL Server中查询表上的属性对
我有两个具有以下模式的表 表A:在SQL Server中查询表上的属性对,sql,sql-server,database,Sql,Sql Server,Database,我有两个具有以下模式的表 表A: (Id(int PK),EmployeeId(int),DepartmentId(int)) 表B: (Id(int PK),EmployeeId(int),DepartmentId(int)) DepartmentId、EmployeeId具有1对1映射,即它们创建一个唯一的对, 表B中的DepartmentId、EmployeeId的映射无效。我想查询表B中的所有Id,其中表A中的同一个EmployeeId在表B中有不同的部门Id。尝试类似的方法 sel
(Id(int PK),EmployeeId(int),DepartmentId(int))
表B:
(Id(int PK),EmployeeId(int),DepartmentId(int))
DepartmentId、EmployeeId具有1对1映射,即它们创建一个唯一的对,
表B中的DepartmentId、EmployeeId的映射无效。我想查询表B中的所有Id,其中表A中的同一个EmployeeId在表B中有不同的部门Id。尝试类似的方法
select *
from tableb b
Where exists (select 1 from tablea a
where a.EmployeeId = b.EmployeeId
and a.DepartmentId <> b.DepartmentId)
试试这样的
select *
from tableb b
Where exists (select 1 from tablea a
where a.EmployeeId = b.EmployeeId
and a.DepartmentId <> b.DepartmentId)
非常感谢您的支持。您可以推荐一个SQL,该SQL通过从表a中获取正确的部门ID来更新表B中的错误部门ID吗?很多原因您可以推荐一个SQL,该SQL通过从表a中获取正确的部门ID来更新表B中的错误部门ID吗?很多原因您可以推荐一个SQL来更新表B中的错误部门ID吗表B通过从表AUPDATE B中获取正确的departmentId设置departmentId=a。departmentId来自a.EmployeeId=B.EmployeeId上的内部联接B,其中a.departmentId!=b、 departmentId非常感谢。您能推荐一个SQL,它通过从表AUPDATE b中获取正确的departmentId来更新表b中错误的departmentId吗?SET departmentId=a.departmentId来自a.EmployeeId=b.EmployeeId的内部联接b,其中a.departmentId!=b、 部门ID
select b.EmployeeId
from a
inner join b on a.EmployeeId = b.EmployeeId
where a.DepartmentId != b.DepartmentId