0

这是我要运行的脚本:

$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 中的“字段列表”中的未知列“总计”

如何使用总行数除以它们的总和以获得平均读数?

4

2 回答 2

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
于 2013-05-15T02:30:54.103 回答
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
于 2013-05-15T02:24:22.560 回答