如何将此左连接查询替换为子查询?
SELECT d.department, COUNT(e.employeeid)
FROM department d
LEFT JOIN employee e
ON d.id = e.employeeid
GROUP BY d.department
ORDER BY d.department;
这是我尝试过的,但不起作用。
SELECT d.department
FROM department AS d
HAVING COUNT(e.employeeid) > 1
(
SELECT COUNT(e.employeeid)
FROM employee AS e
WHERE d.id = e.employeeid
);
答案 0 :(得分:0)
将相关的子查询放入选择列表中。
SELECT d.department,
(select COUNT(e.employeeid) from employee e where d.id = e.employeeid)
FROM department d
ORDER BY d.department
也许你需要做SELECT DISTINCT
以避免重复的行。
编辑:这种加入条件似乎有点奇怪。不应该d.id = e.departmentid
或类似吗?