考虑以下表格:
department
:
deptid (type: INT)
deptname (type: TEXT)
hours (type: INT)
active (type: BIT)
employee
:
empid (type: INT)
empname (type: TEXT)
deptid (type: INT)
designation (type: TEXT)
salary (type: INT)
编写查询以返回属于人数为4或更多的部门的员工的empname
和deptname
列。记录应按empname
的字母顺序返回。
我的解决方案如下。
SELECT e.empname,d.deptname
FROM employee e,department d
WHERE e.deptid=d.deptid
AND d.deptid
IN
(
SELECT deptid
FROM employee
GROUP BY deptid
HAVING COUNT(*)>=4
)
ORDER BY e.empname;
答案 0 :(得分:0)
您的问题的答案可以在this link上找到。
SELECT employee.empname, department.deptname
FROM employee
INNER JOIN department ON employee.deptid = department.deptid
WHERE employee.deptid IN
(
SELECT employee.deptid
FROM employee
GROUP BY employee.deptid
HAVING COUNT(employee.deptid) >= 4
)
ORDER BY employee.empname ASC;
编辑:如果您希望我在此解决方案中解释任何内容,请随时提出。 :)