0

曲棍球应用程序。必须查询表 2 中的所有行并显示玩家姓名而不是玩家 ID

*注意:新的堆栈溢出对不起格式


表一:球员

╔═════════════════════╗
║ id | fname | lname  ║
╠═════════════════════╣
║ 1 | Jason | McFee   ║
║ 2 | John | Smith    ║
║ 3 | Jack | Doe      ║
╚═════════════════════╝

表 2:events_goals

╔═════════════════════════╗
║ id  g_id  a1_id  a2_id  ║
╠═════════════════════════╣
║ 1   1  2  3             ║
║ 2   3  1  2             ║
║ 3   2  1  3             ║
╚═════════════════════════╝

我想要做的是从 events_goals 中获取每一行并将其与匹配的玩家姓名一起返回

我想要完成的内容:球员姓名执行完毕,我不需要在我刚刚放在那里的名字旁边的 (#) 来引用球员 ID

表3

╔════════════════════════════════════════════════╗
║ id  goal  assist1  assist2                     ║
╠════════════════════════════════════════════════╣
║ 1  Jason McFee(1)  John SMith(2)  Jack Doe(3)  ║
║ 2  Jack Doe(3)  Jason McFee(1)  John Smith(2)  ║
║ 3  John Smith(2)  Jason McFee(1)  Jack Doe (3) ║
╚════════════════════════════════════════════════╝

我做了什么

我尝试了一堆不同的查询,这是迄今为止最好的一个

SELECT players.fname AS GFN, 
       players.lname AS GLN 
FROM   events_goals 
       LEFT JOIN players 
              ON events_goals.g_id = players.playerid 

这将返回:

╔═══════════════╗
║ GFN  GLN      ║
╠═══════════════╣
║ Jason  McFee  ║
║ Jack  Doe     ║
║ John  Smith   ║
╚═══════════════╝

我可以为 Assists1 和 Assists 2 运行相同的代码,但是我无法成功地将所有 3 个 SELECT 查询连接在一起,理想情况下,我想将名字和姓氏列加入到 1 个单元格中,但没有那个我可以生活,主要是将所有 ID 转换为名称

4

1 回答 1

2

JOINplayers三次,event_goals因为有三列依赖于players.id.

SELECT  a.ID,
        CONCAT_WS(' ', b.fname, b.lname) goal,
        CONCAT_WS(' ', c.fname, c.lname) assist1,
        CONCAT_WS(' ', d.fname, d.lname) assist2
FROM    event_goals a
        INNER JOIN players b
            ON a.g_id = b.id
        INNER JOIN players c
            ON a.a1_id = c.id
        INNER JOIN players d
            ON a.a2_id = d.id
ORDER   BY a.id
于 2013-10-06T06:54:42.900 回答