我有 5 张桌子:
Stage: ID, stage_name, stage_points, stage_number
Shooter: ID, sh_uid, sh_al, sh_cat
Stage_Shooters: ID, StageID, ShooterID
Score: ID, miss, proc, bth, finalTime, shtr
Stage_Scores: ID, StageID, ScoreID
Stage_Shooters 和 Stage_Scores 是声明表的 many_many 关系。例如,射手通过 Stage_Shooters 连接到舞台。
我正在尝试检索每个阶段的所有射手分数。目前我有 5 个阶段和 28 名射手,因此获得了 140 分。我希望它最终看起来像这样:
--------------------------------------------------------------------------------
| Shooter | Stage Name | Stage Name |
--------------------------------------------------------------------------------
| Alias | Cat | Raw | M | P | B | Fin | Raw | M | P | B | Fin |
--------------------------------------------------------------------------------
| Short Round | Junior | 24.00 | 0 | 0 | 1 | 19.00 | 7.00 | 0 | 0 | 1 | 6.00 |
--------------------------------------------------------------------------------
| Indy Jones | Senior | 24.00 | 0 | 1 | 1 | 22.00 | 4.00 | 0 | 1 | 1 | 12.00 |
--------------------------------------------------------------------------------
为此,我认为我必须做一个数据透视表,但现在我只是想得到以下结果:
-----------------------------------------------------------------------
Stage Name | Shooter Alias | Shooter Category | Raw | M | P | B | Fin |
-----------------------------------------------------------------------
Stage Name | Shooter Alias | Shooter Category | Raw | M | P | B | Fin |
-----------------------------------------------------------------------
当我运行以下 sql 时,我得到 700 个结果而不是 140 个(如预期的那样)
SELECT ssh.stage_name, ssh.sh_al, ssh.sh_cat, ssc.time, ssc.M, ssc.P, ssc.B, ssc.Fin
FROM (
SELECT s.stage_name, sh.sh_al, sh.sh_uid, sh.sh_cat
FROM Shooter sh
INNER JOIN Stage_Shooters stsh ON stsh.ShooterID = sh.ID
INNER JOIN Stage s ON s.ID = stsh.StageID
) ssh
INNER JOIN (
SELECT s.stage_name stageName, sc.time, sc.shtr, sc.miss M, sc.proc P, sc.bth B, sc.finalTime Fin
FROM Score sc
INNER JOIN Stage_Scores stsc ON stsc.ScoreID = sc.ID
INNER JOIN Stage s ON s.ID = stsc.StageID
) ssc
WHERE ssh.sh_uid = ssc.shtr
ORDER BY ssh.sh_al
如果我删除 WHERE 语句,我会得到 19600 个结果。
如何使用联接或子查询来获得我需要的 140 行?