加入3个MySQL表,错误代码1242返回超过1行

时间:2018-03-11 23:57:44

标签: php mysql join html-table

我有以下表格:

高尔夫球手:

| vID | vGolfer |
|-----|---------|
| 1   | Johnson |
| 2   | Thomas  |
| 3   | Rahm    |
| 4   | Spieth  |
| 5   | Rose    |

名册:

| key | vPlayer | vID |
|-----|---------|-----|
| 1   | Player1 | 1   |
| 2   | Player1 | 2   |
| 3   | Player1 | 3   |
| 4   | Player1 | 4   |
| 5   | Player1 | 5   |

其中Roster.vID = Golfers.vID,每位玩家都可以选择5名高尔夫球手参加他们的#34;名单"。

成绩:

| key | vID | vRound | vScore |
|-----|-----|--------|--------|
| 1   | 1   | Rd1    | 71     |
| 2   | 1   | Rd2    | 70     |
| 3   | 1   | Rd3    | 71     |
| 4   | 1   | Rd4    | 69     |

其中Scores.vID = Golfers.vID。

我的目标是展示每个"花名册"在具有以下输出的网页的表格中:

| Player1 | Rd1 | Rd2 | Rd3 | Rd4 |
|---------|-----|-----|-----|-----|
| Johnson | 71  | 68  | 72  | 69  |
| Spieth  | 70  | 70  | 68  | 71  |
| Thomas  | 72  | 71  | 71  | 70  |
| Rahm    | 69  | 68  | 71  | 70  |

我试图使用以下查询:

SELECT
g.vName,  
(select vPlayer from vroster where vID = g.vID) as Player,
(select vScore from vscorecard where vID = g.vID and vRound='Rd1') as Round1
(select vScore from vscorecard where vID = g.vID and vRound='Rd2') as Round2
(select vScore from vscorecard where vID = g.vID and vRound='Rd3') as Round3
(select vScore from vscorecard where vID = g.vID and vRound='Rd4') as Round4
FROM  vgolfers g    

但是我收到错误代码1242.子查询返回超过1行。我理解我试图获得多行,其中vPlayer =" Player1"并且对于每个后续玩家,但我该如何解决这个问题呢?

3 个答案:

答案 0 :(得分:1)

您的示例数据存在很多问题:

  1. 您使用key作为列名。我甚至不知道你是如何设置这样的表,所以我使用了keyx因为这只是一个例子。在这里查找保留字key
  2. 您在每个别名(Round1Round2Round3)后都缺少逗号。
  3. 示例表名称不是您在示例查询中使用的名称(Golfers vs vgolfers; Scores vs vscorecard等等。)
  4. g.vName不存在,但g.vGolfer确实存在。
  5. 总之...

    您没有为名为Rose的高尔夫球手提供分数,因此我只是输入了0来获得分数。

    以下查询将显示您想要的内容:

    SELECT
    g.vGolfer,  
    (select vPlayer from Roster where vID = g.vID) as Player,
    (select vScore from Scores where vID = g.vID and vRound='Rd1') as Round1,
    (select vScore from Scores where vID = g.vID and vRound='Rd2') as Round2,
    (select vScore from Scores where vID = g.vID and vRound='Rd3') as Round3,
    (select vScore from Scores where vID = g.vID and vRound='Rd4') as Round4
    FROM  Golfers g  
    

    enter image description here

    请查看此[DEMO]

    编辑:如果您有其他玩家:

    SELECT
    g.vGolfer,  
    r.vPlayer as Player,
    MAX(CASE WHEN s.vround = 'Rd1' THEN s.vScore ELSE 0 END) AS Rd1,
    MAX(CASE WHEN s.vround = 'Rd2' THEN s.vScore ELSE 0 END) AS Rd2,
    MAX(CASE WHEN s.vround = 'Rd3' THEN s.vScore ELSE 0 END) AS Rd3, 
    MAX(CASE WHEN s.vround = 'Rd4' THEN s.vScore ELSE 0 END) AS Rd4 
    FROM  Golfers g 
    INNER JOIN Roster r ON g.vID = r.vID
    INNER JOIN Scores s ON g.vID = s.vID AND r.keyx = s.keyx
    GROUP BY g.vGolfer, r.vPlayer
    ORDER BY r.vPlayer, g.vGolfer
    

    enter image description here

    [UPDATED DEMO]

答案 1 :(得分:0)

select 
g.vgolfer as player1,
max( case when s.vround = 'Rd1' then s.vscore else 0 end  ) as Rd1,
max( case when s.vround = 'Rd2' then s.vscore else 0 end  ) as Rd2,
max( case when s.vround = 'Rd3' then s.vscore else 0 end  ) as Rd3, 
max( case when s.vround = 'Rd4' then s.vscore else 0 end  ) as Rd4 
from scores s
inner join roster r
on s.vid = r.vid
inner join golfers g
on r.vid = g.vid 
where r.vplayer = 'player1'
group by g.vgolfer

sqlfiddle - 从Brien的scores表中复制了非零测试数据,以验证测试结果。

答案 2 :(得分:0)

以下代码可能会提供您想要的内容。

SELECT vGolfer Player1, 
(SELECT vScore From Scores WHERE Golfers.vID = vID AND vRound = 'Rd1') Rd1,
(SELECT vScore From Scores WHERE Golfers.vID = vID AND vRound = 'Rd2') Rd2,
(SELECT vScore From Scores WHERE Golfers.vID = vID AND vRound = 'Rd3') Rd3,
(SELECT vScore From Scores WHERE Golfers.vID = vID AND vRound = 'Rd4') Rd4
FROM Golfers, Roster
WHERE Golfers.vID = Roster.vID and Roster.vPlayer = 'Player1'

<强>结果

Player1 Rd1 Rd2 Rd3 Rd4
Johnson 71  70  71  69
Thomas  72  71  71  70
Rahm    69  68  71  70
Spieth  70  70  68  71
Rose    (null)  (null)  (null)  (null)

SQL Fiddle