使用 INNER JOIN 将数字从一个表添加到另一个表 - 帮助!
我上网冲浪,发现了与我想要的类似的东西,但它不执行...请帮助
我的表具有以下结构:
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 g1.id = 1;
当我将它放入我的sql中时,它会执行该函数,但它不会改变团队中我的“pointsfavor”和“pointscontra”字段...所以它说,受影响的行(0)...
看:
以下是我在 game 表中输入的值(score1 和 Score2):
这是我执行代码后的 team 表...保持不变:
请帮忙。
I surfed the web and found something similar to what I want but it does not execute...help please
My 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*/
I have this update statement until now:
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 g1.id = 1;
When I put it in my sql it executes the function but it does not alter my 'pointsfavor' and 'pointscontra' fields from team... so it says, affected rows (0)....
Look:
Here are the values that I entered in game table (score1 & score2):
and here is the team table after I excecute the code...is stays the same:
Please Help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两项更新(每队一项):
Two updates (one per team):
使用左连接而不是内连接:
我不知道你的表字段是什么或你的代码如何工作,但我猜你的问题出在你的 if 语句中
例如,在第一次更新(pointsfavor)中,如果 g1.score1 > g1.score2 然后 g1.score1 - g1.score2 并且也在第二个 if 中,但是在第二次更新中,当 g1.score1 > 时g1.score2 然后pointscontra + 0 + 0 和下一个如果也做同样的事情 - 那么在这个查询中只有pointsfavor 受到影响
因此,当第一个子句陈述(pointfavor)正确时,第二个子句陈述就不正确!
再看看你的代码;)
Use left join instead of inner join:
I don't know what's your table fields or how your code works but i guess your problem is in your if statement
For example in first update ( pointsfavor ) IF g1.score1 > g1.score2 then g1.score1 - g1.score2 and also in second if, But in Second update when g1.score1 > g1.score2 Then pointscontra + 0 + 0 and next if also do same thing - then in this query just pointsfavor has affected
So when first clause statement ( pointsfavor ) be correct then second clause statment is incorrect !
take a Look at your code agian ;)