【问题标题】:Help with SQL 2005 and inner join帮助 SQL 2005 和内部联接
【发布时间】:2009-12-18 15:57:22
【问题描述】:

我正在尝试内部连接 ​​2 个临时表
我知道这可以做到,我以前做过,但我完全忘记了如何做到这一点

请告诉我
以下是我尝试执行的查询

select tmp1.*, tmp2.cnt from
(
    select 
        1 as ClassificationType,
        tblMatches.IdGame,
        tblMatches.IdPlayer,
        sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
        count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
    from 
        tblMatches  
    group by IdPlayer, IdGame   
) as tmp1
inner join (select IdWinner, count(IdWinner) as cnt from tblCompetitions where IdWinner = tmp1.IdPlayer) as tmp2 
            on tmp2.IdWinner = tmp1.IdPlayer

这将失败
我想我不允许在创建 tmp2 的子查询中使用 tmp1

消息 4104,第 16 级,状态 1,第 17 行 多部分标识符 无法绑定“tmp1.IdPlayer”。

【问题讨论】:

    标签: sql-server inner-join


    【解决方案1】:

    您不是要连接两个临时表,而是要连接两个派生表。

    除非在 SELECT 子句中,否则您无法访问其外部的一个派生表的内部数据。

    尝试以下方法:

    select tmp1.*, tmp2.cnt from
    (
        select 
            1 as ClassificationType,
            tblMatches.IdGame,
            tblMatches.IdPlayer,
            sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
            count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
        from 
            tblMatches      
        group by IdPlayer, IdGame   
    ) as tmp1
    inner join (select IdWinner, count(IdWinner) as cnt from tblCompetitions GROUP BY IdWinner) as tmp2 
                    on tmp2.IdWinner = tmp1.IdPlayer
    

    【讨论】:

      【解决方案2】:
      select
          1 as ClassificationType,
          tmp1.IdGame,
          tmp1.IdPlayer,
          sum(tmp1.Score) as Score,
          sum(tmp1.Points) as Points,
          sum(tmp1.OpponentScore) as OpponentScore,
          count(tmp1.ID) as MatchesCount,
          count(distinct tmp1.IdCompetition) as CompetitionsCount,
          count(tmp2.IdWinner) as cnt
      from 
          tblMatches tmp1
          inner join
          tblCompetitions tmp2
              on tmp2.IdWinner = tmp1.IdPlayer
      group by
          tmp1.IdPlayer,
          tmp1.IdGame
      

      【讨论】:

        【解决方案3】:

        tmp2 中的 where 子句重复了连接条件:

        inner join (select IdWinner, count(IdWinner) as cnt 
                    from tblCompetitions 
                    where IdWinner = tmp1.IdPlayer) as tmp2 
        on         tmp2.IdWinner = tmp1.IdPlayer
        

        只需删除where 子句。此外,就像 Astander 在他现已删除的帖子中指出的那样,第二个查询也需要 group by:

        inner join (select IdWinner, count(IdWinner) as cnt 
                    from tblCompetitions
                    group by IdWinner) as tmp2 
        on         tmp2.IdWinner = tmp1.IdPlayer
        

        您不能从子查询中引用外部查询的原因是,这会使联接的右侧部分依赖于联接的左侧部分。

        【讨论】:

          【解决方案4】:

          您实际上不应该需要第二个子查询。这个呢?

          SELECT tmp1.ClassificationType, tmp1.IdGame, tmp1.IdPlayer,
                 COUNT(tblCompletions.IdWinner) as cnt FROM
          (
              SELECT 
                  1 as ClassificationType,
                  tblMatches.IdGame,
                  tblMatches.IdPlayer,
                  sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
                  count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
              FROM 
                  tblMatches      
              GROUP BY IdPlayer, IdGame   
          ) as tmp1
          INNER JOIN tblCompletions ON tmp1.IdPlayer = tblCompletions.IdWinner
          GROUP BY tmp1.ClassificationType, tmp1.IdGame, tmp1.IdPlayer
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-01-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-23
            • 1970-01-01
            • 1970-01-01
            • 2010-10-13
            相关资源
            最近更新 更多