开发者

Add numbers from one table field to another?

I want to know how can I add numbers from one table field to another for example, I have:

Table name = Game: 

opponent1(name开发者_Python百科 of row) vs. opponent 2    -  score1 = 25 - score2 =  20

I would like table "team" to update automatically with the following:

Table name = Teams: 

Opponent1: 

  Points in favor = 25
  Points against  = 20

Opponent 2: 

   Points in favor = 20
  Points against  = 25

What would be a code for that? Could it be (has some pseudocode):

  • If score1 bigger than score2
    • Add score1 to "pointsfavor" field in table "team" to opponent1
    • And add $score2 to "pointsagainst" in table "team" to opponent1

Can someone help me please?


Assuming that your tables have the following structure:

TABLE team
  id integer autoincrement primary key,
  name varchar,
  pointsfavor integer,
  pointscontra integer

TABLE game
  id integer autoincrement primary key,
  team1_id integer,
  team2_id integer,
  score1 integer, /*score for team1*/
  score2 integer /*score for team2*/

Your update statement might look something like this:

UPDATE team 
INNER JOIN game g1 ON (team.id = g1.team1_id)
INNER JOIN game g2 ON (team.id = g2.team2_id)
SET pointsfavor = pointsfavor 
      + IF(g1.score1 > g1.score2, g1.score1 - g1.score2, 0) 
      + IF(g2.score2 > g2.score1, g2.score2 - g2.score1, 0)
  , pointscontra = pointscontra 
      + IF(g1.score1 < g1.score2, g1.score2 - g1.score1, 0) 
      + IF(g2.score2 < g2.score1, g2.score1 - g2.score2, 0)
WHERE game.id = 10;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜