此SQL查询有效,但第二个查询不能有人解释为什么和/或使其工作吗?
第一个基于行选择,第二个我希望所有列和行更多地具有全局平均值。该名称可能有1到X个条目,因此必须按名称分组。
SELECT NAME,
AVERAGE,
AD1, AD2, AD3, AD4, AD5, AD6,
(AD1 + AD2 + AD3 + AD4 + AD5 + AD6) AS TOTAL_AD
FROM ( SELECT
NAME,
AVERAGE,
(AVG1-AVERAGE) AS AD1,
(AVG2-AVERAGE) AS AD2,
(AVG3-AVERAGE) AS AD3,
(AVG4-AVERAGE) AS AD4,
(AVG5-AVERAGE) AS AD5,
(AVG6-AVERAGE) AS AD6
FROM P_AVG ) AS T
ORDER BY NAME;
按预期运行。
SELECT NAME,
AVG(AVERAGE),
AD1, AD2, AD3, AD4, AD5, AD6,
(AD1 + AD2 + AD3 + AD4 + AD5 + AD6) AS TOTAL_AD
FROM ( SELECT
NAME,
AVG(AVERAGE),
(AVG(AVG1)-AVG(AVERAGE)) AS AD1,
(AVG(AVG2)-AVG(AVERAGE)) AS AD2,
(AVG(AVG3)-AVG(AVERAGE)) AS AD3,
(AVG(AVG4)-AVG(AVERAGE)) AS AD4,
(AVG(AVG5)-AVG(AVERAGE)) AS AD5,
(AVG(AVG6)-AVG(AVERAGE)) AS AD6
FROM P_AVG GROUP BY NAME ) AS T;
对于该列所在的查询,MySQL返回此错误。
不运行并返回错误。
此问题已解决,但为什么呢?
SELECT NAME,
AD,
AD1, AD2, AD3, AD4, AD5, AD6,
(AD1 + AD2 + AD3 + AD4 + AD5 + AD6) AS TOTAL_AD
FROM ( SELECT
NAME,
(AVG(AVERAGE)) AS AD,
(AVG(AVG1)-AVG(AVERAGE)) AS AD1,
(AVG(AVG2)-AVG(AVERAGE)) AS AD2,
(AVG(AVG3)-AVG(AVERAGE)) AS AD3,
(AVG(AVG4)-AVG(AVERAGE)) AS AD4,
(AVG(AVG5)-AVG(AVERAGE)) AS AD5,
(AVG(AVG6)-AVG(AVERAGE)) AS AD6
FROM P_AVG GROUP BY NAME ) AS T;
答案 0 :(得分:1)
查询中的错误是由于缺少(AVG(AVERAGE))
通常,您是通过外部选择不存在的列名
尝试在此列中添加适当的别名广告,例如:
AVG(AVERAGE) AVERAGE
。
SELECT NAME,
AVG(AVERAGE),
AD1, AD2, AD3, AD4, AD5, AD6,
(AD1 + AD2 + AD3 + AD4 + AD5 + AD6) AS TOTAL_AD
FROM (
SELECT
NAME,
AVG(AVERAGE) AVERAGE,
(AVG(AVG1)-AVG(AVERAGE)) AS AD1,
(AVG(AVG2)-AVG(AVERAGE)) AS AD2,
(AVG(AVG3)-AVG(AVERAGE)) AS AD3,
(AVG(AVG4)-AVG(AVERAGE)) AS AD4,
(AVG(AVG5)-AVG(AVERAGE)) AS AD5,
(AVG(AVG6)-AVG(AVERAGE)) AS AD6
FROM P_AVG
GROUP BY NAME ) AS T;