以下是自我添加Union以来查询的快照。
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'就可以了。
如果我删除联盟并只使用其中一个选择它可以工作(如果我使用FROM fin05_usager_reputation或另一个FROM cc_badge,fin05_usager_badge。
为什么在使用UNION时会出现关于找到idUsager的错误而没有找到联合没有错误?
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等。
答案 0 :(得分: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
这篇文章包含了mysql错误所指的fin05_usager.idusager
的引用。子查询访问只允许一个级别 - 两种方式。没关系,你真的不应该在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
答案 1 :(得分: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。