【问题标题】:I want to insert one item from specified row, to another table我想将指定行中的一项插入另一个表
【发布时间】:2019-05-17 19:43:26
【问题描述】:

所以我有这两张桌子。我需要获取表 1 第 1 行第 3 列中的任何内容(在本例中为鸭子)并将其插入表 2 第 1 行第 6 列(标记为 [此处])

       table 1 (teams)
  PK
team_id   groupno   tname   oname   onumber   oemail   group   series
   1         1      ducks   laura    123      a@b.com    A       1
   2         2      birds   john     456      c@d.com    A       1     
   3         3      redds   hanna    789      e@f.com    A       1
   4         4      blues   mark     102      g@h.com    A       1

team_id 和 groupno 看起来一样,groupno 最多为 10(计算一个组中的所有团队),team_id 继续

       table 2 (games)
  PK
game_id   match_id   time   field   group   team1   team2   series
  1           1      0900     1       A     [here]   N/A      1
  2           2      0930     1       A      N/A     N/A      1
  3           3      1000     1       A      N/A     N/A      1
  4           4      1030     1       A      N/A     N/A      1

game_id 和 match_id 不一样,math_id 上升到 36(计算单系列所有游戏),game_id 继续

低于我一直在使用的东西

UPDATE games INNER JOIN teams
       ON games.game_id = 1
       SET games.team2 = teams.tname
       WHERE teams.series=1;

我想要的结果是什么

例子:

  1. vs 2. --- 3. vs 4.
  2. vs 3. --- 2. vs 4.
  3. vs 4. --- 2. vs 3.

它应该取表 1 中的名字并将它们排列到游戏槽中,以便每个团队互相比赛

 PK
game_id   match_id   time   field   group   team1   team2   series
  1           1      0900     1       A     ducks   birds     1
  2           2      0930     1       A     reds    blues     1
  3           3      1000     1       A     ducks   reds      1
  4           4      1030     1       A     birds   blues     1
  5           5      1100     1       A     ducks   blues     1
  6           6      1130     1       A     reds    birds     1

【问题讨论】:

  • 'WHERE table1.category=1;' - 棘手的是 table1 中没有类别?
  • 请分享table1和table2中的所有字段
  • 这些表之间是什么关系? “表一第三行”和“表二第一行第四列”没有关系
  • 你的意思是行号还是实际ID?解决方案并不总是重叠的。
  • 添加了缺少的“类别”列

标签: mysql


【解决方案1】:

我认为这应该匹配每个组合并将其放在游戏顺序中:

UPDATE games g
INNER JOIN (
    SELECT
        (@cnt := @cnt + 1) game_id, t.team1, t.team2
    FROM (
        SELECT
            DISTINCT t1.tname team1, t2.tname team2
        FROM teams t1
        CROSS JOIN (SELECT DISTINCT tname FROM teams) t2
        WHERE t1.tname <> t2.tname
        ) t
    CROSS JOIN (SELECT @cnt := 0) c
    ) mt ON mt.game_id = g.game_id
SET g.team1 = mt.team1,
    g.team2 = mt.team2
;

虽然我建议使用团队 ID 而不是团队名称。


如果你的意思是 ids 那么:

UPDATE table2 ti
SET ti.item2 = (SELECT t1.item FROM table1 t1 WHERE t1.id = 3)
WHERE ti.id = 1
;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多