MySQL内の相関サブクエリが外部クエリにアクセスできないため、次のSQLが不可能であることがわかりました。以下のクエリを実行すると、「player.playeridnotfound」というエラーが発生します。
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