使用 INNER JOIN 将数字从一个表添加到另一个表 - 帮助!

发布于 2024-11-17 16:50:33 字数 1283 浏览 3 评论 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 g1.id = 1;

当我将它放入我的sql中时,它会执行该函数,但它不会改变团队中我的“pointsfavor”和“pointscontra”字段...所以它说,受影响的行(0)...

看:

我当前的query

以下是我在 game 表中输入的值(score1 和 Score2):

game table

这是我执行代码后的 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:

my current query

Here are the values that I entered in game table (score1 & score2):

game table

and here is the team table after I excecute the code...is stays the same:

team table

Please Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-11-24 16:50:33

两项更新(每队一项):

UPDATE team t1
SET t1.pointsfavor = t1.pointsfavor + IF(game.score1 > game.score2, game.score1 - game.score2, 0)
, t1.pointscontra = t1.pointscontra + IF(game.score2 > game.score1, game.score2 - game.score1, 0)
FROM team
INNER JOIN game ON game.team1_id=t1.id
WHERE game.id = 1

UPDATE team t2
SET t2.pointsfavor = t2.pointsfavor + IF(game.score2 > game.score1, game.score2 - game.score1, 0)
, t2.pointscontra = t2.pointscontra + IF(game.score1 > game.score2, game.score1 - game.score2, 0)    
FROM team
INNER JOIN game ON game.team2_id=t2.id
WHERE game.id = 1

Two updates (one per team):

UPDATE team t1
SET t1.pointsfavor = t1.pointsfavor + IF(game.score1 > game.score2, game.score1 - game.score2, 0)
, t1.pointscontra = t1.pointscontra + IF(game.score2 > game.score1, game.score2 - game.score1, 0)
FROM team
INNER JOIN game ON game.team1_id=t1.id
WHERE game.id = 1

UPDATE team t2
SET t2.pointsfavor = t2.pointsfavor + IF(game.score2 > game.score1, game.score2 - game.score1, 0)
, t2.pointscontra = t2.pointscontra + IF(game.score1 > game.score2, game.score1 - game.score2, 0)    
FROM team
INNER JOIN game ON game.team2_id=t2.id
WHERE game.id = 1
蹲墙角沉默 2024-11-24 16:50:33

使用左连接而不是内连接:

UPDATE team 
LEFT JOIN game g1 ON (team.id = g1.team1_id)
LEFT 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;

我不知道你的表字段是什么或你的代码如何工作,但我猜你的问题出在你的 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:

UPDATE team 
LEFT JOIN game g1 ON (team.id = g1.team1_id)
LEFT 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;

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文