SQL 查询将两列合并为一列

SQL query for joining two columns into one

我有一个 table 的比赛和分数,看起来像这样

match_id | player1 | player2 | player1_score | player2_score |
---------|---------|---------|---------------|---------------|
       1 |       1 |       2 |            30 |            50 |
       2 |       3 |       1 |            35 |            10 |
       3 |       1 |       4 |            40 |            20 |
       4 |       2 |       3 |            20 |            25 |
       5 |       4 |       2 |            65 |            15 |
       6 |       3 |       4 |            10 |            20 |

我想在单列中查询分数

match_id | player | opponent | score |
---------|--------|----------|-------|
       1 |      1 |        2 |    30 |
       2 |      3 |        1 |    35 |
       3 |      1 |        4 |    40 |
       4 |      2 |        3 |    20 |
       5 |      4 |        2 |    65 |
       6 |      3 |        4 |    10 |
       1 |      2 |        1 |    50 |
       2 |      1 |        3 |    10 |
       3 |      4 |        1 |    20 |
       4 |      3 |        2 |    25 |
       5 |      2 |        4 |    15 |
       6 |      4 |        3 |    20 |

这相当于将 table 中的某些列与其他列连接起来:

match_id |  player | opponent |         score |
---------|---------|----------|---------------|
match_id | player1 |  player2 | player1_score |
match_id | player2 |  player1 | player2_score |

但我不确定合适的操作是什么。 join 是我知道的唯一结合 tables 的操作,但这里似乎是错误的工具。

CONCATUNION 似乎都合理,但哪个更可取?查询是什么?

看来你需要一个工会

    select match_id, player1, player2 opponent, player1_score score
    from my_table 
    union all 
    select match_id, player1, player2 , player2_score 
    from my_table