Php 将数字从一个表字段添加到另一个表字段?
我想知道如何将数字从一个表字段添加到另一个表字段。例如,我有:Php 将数字从一个表字段添加到另一个表字段?,php,mysql,html,phpmyadmin,Php,Mysql,Html,Phpmyadmin,我想知道如何将数字从一个表字段添加到另一个表字段。例如,我有: Table name = Game: opponent1(name of row) vs. opponent 2 - score1 = 25 - score2 = 20 我希望表“team”自动更新为以下内容: Table name = Teams: Opponent1: Points in favor = 25 Points against = 20 Opponent 2: Point
Table name = Game:
opponent1(name of row) vs. opponent 2 - score1 = 25 - score2 = 20
我希望表“team”自动更新为以下内容:
Table name = Teams:
Opponent1:
Points in favor = 25
Points against = 20
Opponent 2:
Points in favor = 20
Points against = 25
这方面的代码是什么?可能是(有一些伪代码):
- 如果分数1大于分数2
- 将得分1添加到对手1表“团队”中的“得分”字段
- 并在“团队”表中的“pointsagainst”中添加$score2到opponent1
有人能帮我吗?假设您的表具有以下结构:
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语句可能如下所示:
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;
更新团队
内部加入游戏g1开启(team.id=g1.team1\u id)
内部加入游戏g2开启(team.id=g2.team2\u id)
设置点SAVOR=点SAVOR
+如果(g1.score1>g1.score2,g1.score1-g1.score2,0)
+如果(g2.score2>g2.score1,g2.score2-g2.score1,0)
,pointscontra=pointscontra
+如果(g1.score1
thnx。。这是做什么的:WHERE game.id=10;?它只更新第10场比赛的分数,如果你不添加where
子句,它将继续更新它已经处理过的分数,但是如果你在一场比赛结束后立即更新分数,你就不需要跟踪哪些比赛已经处理过他们的分数。所以在game.id=“10”中我用“$id”来代替,对吗?我会替约翰回答的,是的!(假设您的“$ID”实际上是游戏桌的ID,而不是说团队桌的ID,完全不同的东西)。这不是批评,但一定要学习基本SQL,你很快就会喜欢它的…@Norman,为了扩展@virtualeyes,我建议将变量命名为$game\u id
,这样就不会混淆你所指的id,6个月后,当您忘记了所有那些现在在您脑海中清晰可见的细节时,当您查看您的代码时,您将为这样做感到自豪。