SQL更新计数
SQL UPDATE with having count
我有这个table
A B
1 1H
2 1H
1 1G
1 1E
我需要更新此 table 列 A,将 max(a) 按 B 分组,计数 (*) = 2。
结果将是这样的:
A B
2 1H
2 1H
1 1G
1 1E
也许吧?
update tablename t1
set A = (select max(A) from tablename t2 where t2.B = t1.B)
where B in (select B from tablename group by B having count(*) >= 2)
你可以试试下面
update tablename t1
set A = (select count(*) from tablename t2 where t2.B = t1.B having count(*)=2)
我有这个table
A B
1 1H
2 1H
1 1G
1 1E
我需要更新此 table 列 A,将 max(a) 按 B 分组,计数 (*) = 2。
结果将是这样的:
A B
2 1H
2 1H
1 1G
1 1E
也许吧?
update tablename t1
set A = (select max(A) from tablename t2 where t2.B = t1.B)
where B in (select B from tablename group by B having count(*) >= 2)
你可以试试下面
update tablename t1
set A = (select count(*) from tablename t2 where t2.B = t1.B having count(*)=2)