如何使用subsery更新列

时间:2018-04-12 01:42:40

标签: mysql sql oracle oracle11g

我在下面有一个EMPLOYEE表:

EMP_ID     DEPT_ID
101        1
102        2
103        3
104        1

DEPARTMENT表格如下:

DEPT_ID  COUNTS
1   
2   
3   

我想编写一个查询,该查询将计算属于某个部门的Employee的数量并将其存储到Department列表中,以便Department表看起来像:

DEPT_ID  COUNTS
1         2
2         1
3         1

我试过了:

update department p
set p.counts = (select count(*) from EMPLOYEE e where p.dept_id = e.dept_id
group by e.dept_id)

但它不起作用。

1 个答案:

答案 0 :(得分:2)

删除group by和第一个别名:

update department p
    set counts = (select count(*) from EMPLOYEE e where p.dept_id = e.dept_id);