我有一张桌子,它有以下几列:
SchoolName ClassSize
---------- ---------
A NULL
A NULL
A NULL
B NULL
B NULL
我想根据学校名称的数量在ClassSize列中设置值,例如:
SchoolName ClassSize
---------- ---------
A 3
A 3
A 3
B 2
B 2
请让我知道该怎么做。
我正在做这样的事情:
update table as set ClassSize = (select count(SchoolName) from table group by SchoolName);
答案 0 :(得分:2)
您可以通过将表与返回计数器的查询连接起来来实现:
update tablename t
inner join (
select schoolname, count(*) counter
from tablename
group by schoolname
) g on g.schoolname = t.schoolname
set t.ClassSize = g.counter;
请参见demo。
结果:
| SchoolName | ClassSize |
| ---------- | --------- |
| A | 3 |
| A | 3 |
| A | 3 |
| B | 2 |
| B | 2 |