我发现由于MySQL中的相关子查询无法访问外部查询,因此无法使用以下SQL,当我运行以下查询时,我得到了"播放器的错误。未找到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
答案 0 :(得分: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 FROM drafts WHERE type = 3)”是常量,你可以:
SELECT COUNT(drafts.id)FROM草稿WHERE type = 3到@total_mocks;
然后使用@total_mocks替换表达式“(SELECT COUNT(drafts.id)FROM drafts WHERE type = 3)”
试试吧