代码:

select d.Name as Department,e.Name as Employee,e.Salary
from Department as d,Employee as e
where d.Id=e.DepartmentId 
      and d.Id in (select max(Salary),Employee.DepartmentId 
                   from Employee
                   group by DepartmentId);

报错:

Operand should contain 1 column(s)

原因:

在子查询中,查出的数据是有两个字段的,而where语句中,“ ... d.Id in ....”,只有一个字段进行查询。也就是说,前后的字段数量不匹配,在使用in谓词时,in左右两边的字段数量要相同。将上面的代码改成如下可以成功。

select d.Name as Department,e.Name as Employee,e.Salary
from Department as d,Employee as e
where d.Id=e.DepartmentId 
      and (e.Salary, d.Id) in (select max(Salary),Employee.DepartmentId 
                   from Employee
                   group by DepartmentId);

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐