2011-05-22 83 views
0

我想知道我如何从一个表字段添加号码到另一个例子,我有:将数字从一个表格字段添加到另一个表格字段中?

Table name = Game: 

opponent1(name of row) vs. opponent 2 - score1 = 25 - score2 = 20 

我想表“团队”与下面的自动更新:

Table name = Teams: 

Opponent1: 

    Points in favor = 25 
    Points against = 20 

Opponent 2: 

    Points in favor = 20 
    Points against = 25 

这是什么代码?难道是(有一些伪代码):

  • 如果score1大于score2
    • 添加score1为 “pointsfavor” 字段表 “团队”,以opponent1
    • 而在加$ score2为 “pointsagainst”表 “团队”,以opponent1

有人能帮助我吗?

回答

0

假设你的表有以下结构:

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*/ 

你的更新语句可能是这个样子:

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

日Thnx! ..这是做什么的:WHERE game.id = 10;? – Norman 2011-05-22 13:24:49

+0

它只会更新游戏编号10的分数,如果您不添加“where”子句,它会继续更新已经处理的分数,但是如果您在游戏结束后立即更新分数,则不会需要跟踪哪些游戏处理了他们的分数。 – Johan 2011-05-22 13:26:46

+0

so in game.id =“10”我替换“$ ID”吧? – Norman 2011-05-22 13:33:07

相关问题