假设我有这张表:
+-----+------+------------+
| eid | dept | joining |
+-----+------+------------+
| 1 | IT | 1982-01-13 |
| 2 | IT | 1983-01-13 |
| 3 | CSE | 1984-04-13 |
| 4 | CSE | 1983-03-23 |
| 5 | IT | 1985-03-23 |
| 6 | ECE | 1986-03-23 |
| 7 | ECE | 1986-11-23 |
+-----+------+------------+
现在我想获取1983年1月1日之后加入的每个部门的员 worker 数记录。
我尝试了这个查询:
select count(a.eid) as total, dept
from (
select *
from t1
where dept = 'IT'
having DATE(joining) > '1982-01-01'
) as a;
+-------+------+
| total | dept |
+-------+------+
| 3 | IT |
+-------+------+
但我需要将所有部门放在一张表中。
select joining, dept, count(*)
from t1
group by dept
having(joining) > '1982-01-01'
order by joining;
没有这样的结果。
最佳答案
您需要一个简单的 group by 子句,其中包含一个 where 来过滤您想要的日期,如下所示:
select dept,count(*)
from t1
where joining > '1982-01-01'
group by dept
关于java - 如何获取 1983 年 1 月 1 日之后加入的部门员工数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35911096/