gpt4 book ai didi

带有 5 个表的 mySQL INNER JOIN

转载 作者:行者123 更新时间:2023-11-29 01:44:04 25 4
gpt4 key购买 nike

我有 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 是陈述表的多对多关系。例如射手通过 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 行?

最佳答案

我有 3 个建议:

1- 你没有加入相应阶段的数据

如果您将 AND ssh.stage_name = ssc.stage_name 添加到查询中,只要您没有多个具有相同名称的阶段,它就应该可以工作。否则,您应该在两个子查询上添加舞台的 ID,并使用这些 ID 而不是舞台名称将条件添加到 where。

2- 你缺少子查询之间的连接条件

之所以需要添加 WHERE 条件或者得到 20K 的结果是因为 INNER JOIN 没有 ON。也就是说,您可以将在 WHERE 上添加的所有内容都放在 ON 上。

像这样:

SELECT ssh.stage_name, ssh.sh_al, ssh.sh_cat, ssc.time, ssc.M, ssc.P, ssc.B, ssc.Fin
FROM (
SELECT s.ID stageID, 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.ID stageID, 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
ON ssh.sh_uid = ssc.shtr AND ssh.stageID = ssc.stageID;

3- 你可能可以用一个查询完成整个事情

看起来您可以获得与以下(较短的)查询完全相同的结果:

SELECT
stage_name,
sh_al,
sh_cat,
time,
miss AS M,
proc AS p,
bth AS b,
finalTime AS Fin
FROM
Shooter AS sh
JOIN Stage_Shooters AS stsh ON stsh.ShooterID = sh.ID
JOIN Stage AS s ON s.ID = stsh.StageID
JOIN Stage_Scores AS stsc ON stsc.StageID = s.ID
JOIN Score AS sc ON stsc.ScoreID = sc.ID AND sc.shtr = sh.ID

如果没有 SQL Fiddle,实际验证这一点很复杂,但我非常有信心它应该可以工作。

关于带有 5 个表的 mySQL INNER JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11731515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com