0

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 
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

「(SELECTCOUNT(drafts.id)FROMdrafts WHERE type = 3)」が定数の場合、次のことができます。

SELECT COUNT(drafts.id)FROMドラフトWHERE type = 3 into @total_mocks;

次に、@ total_mocksを使用して、式 "(SELECT COUNT(drafts.id)FROMdrafts WHERE type = 3)"を置き換えます。

やってみなよ

于 2012-07-13T04:45:08.047 に答える