0

我发现以下 SQL 是不可能的,因为 MySQL 中的相关子查询无法访问外部查询,当我运行以下查询时,我收到“未找到 player.playerid”的错误

SELECT player.position, player.playerid, adp.average_draft_position FROM player, (
    SELECT ((ad_p.mult + ad_p.pick_sum) / ad_p.total_mocks) AS average_draft_position FROM (
        SELECT draft_c.total_mocks as total_mocks, 
               ((draft_c.total_mocks - player_c.total_picks) * 252) AS mult, 
               SUM(overall_pick) AS pick_sum FROM draft_history JOIN drafts ON drafts.id = draft_history.draftid, 
            (
                SELECT COUNT(drafts.id) AS total_mocks FROM drafts WHERE type = 3
            ) AS draft_c, (
                SELECT COUNT(draft_history.playerid) AS total_picks FROM draft_history WHERE draft_history.playerid = player.playerid
            ) AS player_c WHERE draft_history.playerid = player.playerid
    ) AS ad_p
) AS adp WHERE player.sportid = 1 AND player.position IN ('x', 'y', 'z') ORDER BY adp.average_draft_position ASC

我试图在上面使用以下作为子查询,

SELECT ((adp.mult + adp.pick_sum) / adp.total_mocks) AS average_draft_position FROM (
            SELECT draft_c.total_mocks as total_mocks, ((draft_c.total_mocks - player_c.total_picks) * 252) AS mult, SUM(overall_pick) AS pick_sum FROM draft_history JOIN drafts ON drafts.id = draft_history.draftid, 
            (
                SELECT COUNT(id) AS total_mocks FROM drafts WHERE type = :type
            ) AS draft_c, (
                SELECT COUNT(playerid) AS total_picks FROM draft_history WHERE playerid = :playerid
            ) AS player_c WHERE playerid = :playerid
        ) as adp

这将确定用户在整个历史中被选中的平均位置,包括未通过对总和应用权重来选择用户的情况,这是有效的。

上面的第一个查询尝试获取并选择当前存在于给定位置的所有用户,获取平均位置并按其排序。

我不太擅长 SQL,所以我可能会做得更好,如果我能更好地解释这一点,请告诉我。

用给定的查询解决了问题,

SELECT position, 
       playerid,
       ((((adp.total_mocks - adp.participated_mocks) * 252) + adp.pick_sum) / adp.total_mocks) AS average_draft_position
FROM 
(      
    SELECT player.position, 
           player.playerid,
           (SELECT COUNT(drafts.id) FROM drafts WHERE type = 3) as total_mocks,
           COUNT(draft_history.playerid) as participated_mocks,
           SUM(overall_pick) AS pick_sum 
    FROM draft_history 
    INNER JOIN drafts 
      ON drafts.id = draft_history.draftid
    INNER JOIN player
      ON player.sportid = 1
     AND player.position IN ('QB') 
     AND draft_history.playerid = player.playerid
    GROUP BY player.position, player.playerid
) adp ORDER BY average_draft_position ASC 
4

1 回答 1

1

我想我可以理解这一点。尝试这个:

select position, 
       playerid,
       ((adp.mult + adp.pick_sum) / adp.total_mocks) AS average_draft_position
from 
(      
    SELECT player.position, 
           player.playerid,
           (SELECT COUNT(drafts.id) FROM drafts WHERE type = 3) as total_mocks
           (
                (SELECT COUNT(drafts.id) FROM drafts WHERE type = 3) 
              - COUNT(draft_history.playerid)
           )*252 as mult,
           SUM(overall_pick) AS pick_sum 
    FROM draft_history 
    INNER JOIN drafts 
      ON drafts.id = draft_history.draftid
    INNER JOIN player
      ON player.sportid = 1
     AND player.position IN ('x', 'y', 'z') 
     AND draft_history.playerid = player.playerid
    group by player.position, player.playerid
) adp
order by 3 asc

如果 "(SELECT COUNT(drafts.id) FROM drafts WHERE type = 3)" 是一个常数,你可以:

SELECT COUNT(drafts.id) 从草稿 WHERE 类型 = 3 到 @total_mocks;

然后使用@total_mocks 替换表达式“(SELECT COUNT(drafts.id) FROM drafts WHERE type = 3)”

去尝试一下

于 2012-07-13T04:45:08.047 回答