这是我正在尝试运行的脚本:
$resultadosPromediados = R::getAll("SELECT count(*) as total,
SUM(dialogue_score) / total as dialogue_score,
SUM(consulta_general_score) / total as consulta_general_score,
SUM(tarea_score) / total as tarea_score,
SUM(foro_calificacion_score) / total as foro_calificacion_score,
SUM(foro_respuesta_score) / total as foro_respuesta_score
FROM ranking
WHERE created = :ultima_feha",
array(':ultima_feha' => $fecha));
print_r($resultadosPromediados);
但是我收到了这个错误:
致命错误:未捕获[42S22] - SQLSTATE [42S22]:未找到列:1054第265行/home/ASDFASDF/public_html/ADSFADSF/rb.php中的“字段列表”中未知列“总计”
如何使用总行来划分它们的总和以获得平均读数?
答案 0 :(得分:2)
只需使用变量:
SELECT @total := count(*),
SUM(dialogue_score) / @total as dialogue_score,
SUM(consulta_general_score) / @total as consulta_general_score,
SUM(tarea_score) / @total as tarea_score,
SUM(foro_calificacion_score) / @total as foro_calificacion_score,
SUM(foro_respuesta_score) / @total as foro_respuesta_score
FROM ranking
WHERE created = :ultima_feha
答案 1 :(得分:1)
您无法使用在ALIAS
语句的同一级别生成的SELECT
。你必须选择
一,使用整个表达式,
SELECT count(*) as total,
SUM(dialogue_score) / count(*) as dialogue_score,
SUM(consulta_general_score) / count(*) as consulta_general_score,
SUM(tarea_score) / count(*) as tarea_score,
SUM(foro_calificacion_score) / count(*) as foro_calificacion_score,
SUM(foro_respuesta_score) / count(*) as foro_respuesta_score
FROM ranking
WHERE created = :ultima_feha
其次,要使用子查询,
SELECT total,
dialogue_total / total AS dialogue_score,
consulta_general_total / total AS consulta_general_score,
tarea_total / total AS tarea_score,
foro_calificacion_total / total AS foro_calificacion_score,
foro_respuesta_total / total AS foro_respuesta_score
FROM
(
SELECT count(*) as total,
SUM(dialogue_score) as dialogue_total,
SUM(consulta_general_score) as consulta_general_total,
SUM(tarea_score) as tarea_total,
SUM(foro_calificacion_score) as foro_calificacion_total,
SUM(foro_respuesta_score) as foro_respuesta_total
FROM ranking
WHERE created = :ultima_feha
) alias