1

这是自从我添加联合以来不起作用的查询的快照。

SELECT fin05_usager.idUsager,
       (SELECT sum(nombreReputation) as nombreReputation
          FROM (SELECT SUM(nombreReputationGagner) as nombreReputation
                 FROM fin05_usager_reputation 
                WHERE fin05_usager_reputation.idUsager = fin05_usager.idUsager
             GROUP BY fin05_usager_reputation.idUsager
        UNION
                SELECT SUM(cc_badge.valeurEnReputation) as nombreReputation
                  FROM cc_badge, fin05_usager_badge
                 WHERE fin05_usager_badge.idBadge = cc_badge.idBadge
                   AND fin05_usager_badge.idUsager = fin05_usager.idUsager) as repuUnion    
       ) as repu
 FROM fin05_usager
WHERE fin05_usager.idUsager = 6

错误是:#1054 - 'where 子句'中的未知列'fin05_usager.idUsager'

如果我删除 fin05_usager.idUsager 并直接使用“6”,它确实有效。

如果我删除联合并仅使用 2 个选择中的一个,则它可以工作(如果我使用 FROM fin05_usager_reputation 或另一个 FROM cc_badge、fin05_usager_badge。

为什么在使用 UNION 时会出现查找 idUsager 的错误,而没有 union 则找不到错误?

架构简化:

fin05_usager: idUsager int(8)

fin05_usager_reputation : idUsager int(8), nombreReputationGagner int(4)

cc_badge : idBadge int(4), valeurEnReputation int(4)

fin05_usager_badge : idUsager int(8), idBadge int(4)

笔记:

我不能直接在查询中执行子查询。我必须在 select 的子查询中使用它,因为实际上,查询非常大并且已经包含 Group 等。

4

2 回答 2

2

当您提到删除 UNION 会导致工作正常时,我敢打赌您正在专门删除它:

UNION
SELECT SUM(cc_badge.valeurEnReputation) as nombreReputation
  FROM cc_badge, fin05_usager_badge
 WHERE fin05_usager_badge.idBadge = cc_badge.idBadge
   AND fin05_usager_badge.idUsager = fin05_usager.idUsager

这篇文章包含对fin05_usager.idusagermysql 错误所指的引用。子查询访问只允许一个级别 - 两种方式。没关系,您真的不应该在 SELECT 子句中执行 SELECT。以下是我重新解释您的查询的方式:

SELECT fu.idUsager,
       a.nombreReputation + b.nombreReputation AS repuunion
  FROM fin05_usager fu
  JOIN (SELECT fur.idusager,
               SUM(fur.nombreReputationGagner) as nombreReputation
          FROM fin05_usager_reputation fur 
         WHERE fur.idUsager = fin05_usager.idUsager
      GROUP BY fur.idUsager) a ON a.idusager = fu.idusager
  JOIN (SELECT fub.idUsager,
               SUM(ccb.valeurEnReputation) as nombreReputation
          FROM cc_badge ccb
          JOIN fin05_usager_badge fub ON fub.idbadge = ccb.idbadge
      GROUP BY fub.idUsager) b ON b.idusager = fu.idusager
 WHERE fu.idUsager = 6
于 2009-10-18T23:18:46.847 回答
0

好的,

目前我发现的唯一方法是在主查询中不执行 Group By(因为在实际情况下我不能,因为它确实包含 Group By 子句)是在 2 中执行并将其与代码合并...不是我想要的,但它至少有效:

    SELECT fin05_usager.idUsager,

                (SELECT sum(nombreReputationGagner) as nombreReputation1
                FROM fin05_usager_reputation 
                WHERE fin05_usager_reputation.idUsager = fin05_usager.idUsager
                group by fin05_usager_reputation.idUsager) as nombreReputation1
            ,
                (SELECT sum(cc_badge.valeurEnReputation) as nombreReputation2
                FROM cc_badge,  fin05_usager_badge
                WHERE fin05_usager_badge.idBadge = cc_badge.idBadge
                AND fin05_usager_badge.idUsager = fin05_usager.idUsager) as nombreReputation2

    FROM 
        fin05_usager
        where fin05_usager.idUsager = 6

在代码中,我将 nombreReputation1 和 nombreReputation2 相加。

于 2009-10-18T23:56:26.847 回答