Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 根据条件更新SQL表。_Mysql - Fatal编程技术网

Mysql 根据条件更新SQL表。

Mysql 根据条件更新SQL表。,mysql,Mysql,这是我的桌子结构 Teams (Table Name) TeamID TeamName TotalScore 01 Cowboys 6 02 Bulldogs 03 Broncos 04 Raiders Games (Table Name) GameID team1 team2 team1score team2score Winner GameStart

这是我的桌子结构

Teams (Table Name) 
TeamID      TeamName       TotalScore 
01          Cowboys        6 
02          Bulldogs 
03          Broncos 
04          Raiders

Games (Table Name) 
GameID    team1     team2     team1score team2score Winner GameStart            Status 
01        01        03                                     02/04/2014 07:00:00    0 
02        02        04        6          10         04     03/04/2014 05:30:00    2

Users (Table Name) 
userID      Username      Password    Main Team   Points 
01          Brodey        abc         02          3

Tips (Table Name) 
TipID     userID      GameID       TippedTeam 
01        01          01           02 
02        01          02           03
如果
游戏状态=2
检查
提示
。如果
TippedTeam
Games
中的获胜者匹配,则更新
用户积分+3
。如果获胜者不匹配,请不要更新

我试过这个,但不起作用:

update users as u,  tips as t 
set u.points = u.points +3
where u.userid = t.userid 
and tips.tippedteam = (select winner from games where status=2)

请帮助加入所有3个表,尝试以下操作:

update users as u, tips, games as g
set u.points = u.points + 3 
where u.userid = tips.userid 
and tips.gameid = g.gameid
and tips.tippedteam = g.winner 
and g.status=2
update teams
join games
on teams.teamID = games.team1
set totalscore = CASE WHEN teams.teamID = games.team1 THEN IFNULL(TotalScore,0)+games.team1score ELSE TotalScore END
where games.status = 2;

update teams
join games
on teams.teamID = games.team2
set totalscore = CASE WHEN teams.teamID = games.team2 THEN IFNULL(TotalScore,0)+games.team2score ELSE TotalScore END
where games.status = 2
对于您的第二个请求(在评论中提出),请尝试以下操作:

update users as u, tips, games as g
set u.points = u.points + 3 
where u.userid = tips.userid 
and tips.gameid = g.gameid
and tips.tippedteam = g.winner 
and g.status=2
update teams
join games
on teams.teamID = games.team1
set totalscore = CASE WHEN teams.teamID = games.team1 THEN IFNULL(TotalScore,0)+games.team1score ELSE TotalScore END
where games.status = 2;

update teams
join games
on teams.teamID = games.team2
set totalscore = CASE WHEN teams.teamID = games.team2 THEN IFNULL(TotalScore,0)+games.team2score ELSE TotalScore END
where games.status = 2
将更新查询(如上)合并为一个:

update teams
join games
on (teams.teamID = games.team1 or teams.teamID = games.team2)
set totalscore = 
      CASE 
      WHEN teams.teamID = games.team1 THEN IFNULL(TotalScore,0)+games.team1score 
      WHEN teams.teamID = games.team2 THEN IFNULL(TotalScore,0)+games.team2score 
      ELSE TotalScore 
      END
where games.status = 2

你好,我还有一个问题…我也写不出来。。。当游戏中的状态=2时,每次都需要用team1score更新team1,用team2score和+更新team2。不是替换而是增加,例如本场比赛6个进球,但下一场比赛10个,总分应该是16。你能帮我解决这个问题吗query@AnkurDhanuka当然可以,但我不能完全理解你的第二个要求。请使用链接在您的问题中添加更多详细信息(包括样本数据和预期结果)。谢谢。在此数据库中…我们有gameid 02,其状态为2…现在应该发生什么…team1为02,team1score为6…此分数应添加到团队表中,即在团队表中,分数为0的teamsID 02将变为0+6…对于团队2和team2score@AnkurDhanuka我已经更新了上面的答案,询问您的第二个请求。我还没有测试过,让我知道它是否适合你。