简体   繁体   中英

Mysql relating two tables with functions

I have two tables one contains a list of teams, the other contains a list of schedules for these teams and score results. I want to be able to update the teams table when I insert a row into the schedules table. So for example if I have:

Teams table

Team1
Team2

.

Schedules table

team1, 3
team2, 1

what I want to be able to do is when the score fields are updated I need to update different fields in the teams table. So when I update this schedule row,

It would insert 3 into one of the rows for team1, and 1 for the team2

Also I would like to be able to calculate the difference between the scores and also insert this into the teams table.

What would be the best way to implement this?

And I would need a function in order to do the comparison of the scores right?

Thanks,

So far I thought of doing something like this.

update table teams set teams.gamesplayed = teams.gameplayed +1  /*this would add one to the games played field because they just played.*/

then for the goals scored something like

update teams set teams.gf =
(

select t.goalsscored
from schedule t, teams s
where t.teamname = s.team1name AND )

)

you need add ON UPDATE and ON INSERT triggers to Teams table - thats all. In trigger you can do anything you need. Of course you can create standalone function and call it from triggers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM