Help with a trigger

Not a trigger expert... I would like to sum the num of confirmed players in the games table each time a player record is created, updated or deleted. Here’s my non-working code:

drop function if exists games_players cascade;

create or replace function games_players()
returns trigger as $$ begin
update games set players_conf = (select sum(players.num) AS num from players where players.game_id = NEW.game_id group by players.game_id) where games.id = NEW.game_id;
return NEW;
end;
$$ language plpgsql;


Any ideas?
Was this page helpful?