我在“reportsTo”中的内容与employeeNumber类似,那么问题是什么?
SELECT firstName,lastName,temp.count
FROM employees
where employeeNumber in(
SELECT reportsTo, COUNT( * ) AS count
FROM employees
GROUP BY reportsTo
) as temp
答案 0 :(得分:2)
用于IN子句的子查询无法返回多个列,因此您无法在其中包含SELECT reportsTo, COUNT( * ) AS count
。您可能希望加入子查询,如下所示:
SELECT firstName,lastName,temp.count
FROM employees
INNER JOIN (
SELECT reportsTo, COUNT( * ) AS count
FROM employees
GROUP BY reportsTo
) temp ON employees.employeeNumber = temp.reportsTo
免责声明:我不使用MySQL,因此上述语法可能不准确。
您可能也会在尝试使用count
作为列名时遇到问题。重命名或转义它(SQL Server使用[
和]
,不确定MySQL。)
答案 1 :(得分:0)
不知道你想要什么,只需将其改为:
SELECT firstName,
lastName
FROM employees
WHERE employeeNumber in( SELECT reportsTo
FROM employees
GROUP BY reportsTo )
删除计数和别名会有效,但我觉得你真正想要的是:
SELECT manager.firstName,
manager.lastName,
temp.count AS number_of_directs
FROM (SELECT reportsTo,
COUNT( * ) AS count
FROM employees
GROUP BY reportsTo) as temp
INNER JOIN employees AS manager ON manager.reportsTo = temp.reportsTo