Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-基于分组计数更新特定行_Sql_Postgresql - Fatal编程技术网

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
     )
)

你的预期产出是什么?你的预期产出是什么?