define a computed column reference another table

后端 未结 2 823
既然无缘
既然无缘 2020-12-16 05:44

I have two database tables, Team (ID, NAME, CITY, BOSS, TOTALPLAYER) and Player (ID, NAME, TEAMID, AGE), the rel

2条回答
  •  情话喂你
    2020-12-16 06:13

    You don't have to store the total in the table -- it can be computed when you do a query, something like:

    SELECT teams.*, COUNT(players.id) AS num_players
    FROM teams LEFT JOIN  players ON teams.id = players.team_id
    GROUP BY teams.id;
    

    This will create an additional column "num_players" in the query, which will be a count of the number of players on each team, if any.

提交回复
热议问题