SQL-基于分组计数更新特定行
如果一组行属于某个组的结果,我希望通过+具有条件来更新这些行: 示例表:SQL-基于分组计数更新特定行,sql,postgresql,Sql,Postgresql,如果一组行属于某个组的结果,我希望通过+具有条件来更新这些行: 示例表: equip_id | fiber_id | new_col -----+------------------------------------------------------------------- A | 1 | *update this row/col because there are (3) A's A | 2 | *update this row/c
equip_id | fiber_id | new_col
-----+-------------------------------------------------------------------
A | 1 | *update this row/col because there are (3) A's
A | 2 | *update this row/col because there are (3) A's
A | 3 | *update this row/col because there are (3) A's
B | 1 | *dont update this row/col because there are (4) B's
B | 2 | *dont update this row/col because there are (4) B's
B | 3 | *dont update this row/col because there are (4) B's
B | 4 | *dont update this row/col because there are (4) B's
我只想在按装备id分组且计数(装备id)=3的情况下更新新的装备列
由于更新中不支持GROUP BY,如何实现此结果?您可以使用以下嵌套表表达式:
update table_name
set ...
where equip_id in (
select equip_id
from table_name
group by equip_id
having count(*) = 3
)
可以使用以下嵌套表表达式:
update table_name
set ...
where equip_id in (
select equip_id
from table_name
group by equip_id
having count(*) = 3
)
安德洛尼卡已经给出了正确的答案 下面是另一种方式。不确定它是否更有效
update table_name set new_col=NEW_VALUE
where equip_id in
(
select equip_id from table_name a
where 3=
(
select count(*) from table_name b where
a.equip_id=b.equip_id
)
)
安德洛尼卡已经给出了正确的答案 下面是另一种方式。不确定它是否更有效
update table_name set new_col=NEW_VALUE
where equip_id in
(
select equip_id from table_name a
where 3=
(
select count(*) from table_name b where
a.equip_id=b.equip_id
)
)
你的预期产出是什么?你的预期产出是什么?