使用AVG()的嵌套SQL语句

时间:2015-06-09 05:03:11

标签: php mysql select

我有三个表要从中提取数据。一个是一个类表,它存储一个提供的类列表,包括id,时间戳,培训师ID,开始时间,上午/下午和课程举行的日期。培训师ID是一个外键,将其与培训师表联系起来,我根据培训师的唯一ID来提取培训师姓名。

这很好,但我还需要显示每个班级的平均出勤率。这涉及一个stats表,它具有一个class id外键,它将class id与该会话的参与者数一起放入一行。我想返回每个班级每班课程的平均参与人数。

以下是我的选择声明:

SELECT 
                        class_id AS id, 
                        class_name, 
                        class_trainerid,
                        class_starttime AS start, 
                        class_ampm AS ampm,
                        class_days AS days,
                        trainer_id AS trainid,
                        trainer_name,
                        stat_class AS sclass,
                        AVG(stat_students) as stat_students_avg
                    FROM 
                        $class_table
                        LEFT JOIN $trainer_table ON (class_trainerid = trainer_id)
                        LEFT JOIN stats ON (id = stat_students_avg)
                    GROUP BY
                        id

上面的代码可能表明我实际上并不知道如何做到这一点。我已经看过关于通过连接求平均的帖子,或者在select语句中使用select语句,但我似乎无法将这些转换为我的问题。

编辑: 这是类表模式:

`class_id` tinyint(8) NOT NULL AUTO_INCREMENT,
`class_datereated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`class_name` varchar(256) NOT NULL,
`class_trainerid` tinyint(8) NOT NULL,
`class_starttime` time NOT NULL,
`class_ampm` text NOT NULL,
`class_days` text NOT NULL,
PRIMARY KEY (`class_id`)

这是Trainer架构

`trainer_id` tinyint(8) NOT NULL AUTO_INCREMENT,
'trainer_datecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`trainer_name` varchar(256) NOT NULL,
`trainer_password` varchar(25) NOT NULL,
`trainer_email` varchar(256) NOT NULL,
`trainer_active` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`trainer_id`),
UNIQUE KEY `email` (`trainer_email`)

这是stats表架构:

`stat_id` int(8) NOT NULL AUTO_INCREMENT,
`stat_datecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`stat_class` int(8) NOT NULL,
`stat_students` int(8) NOT NULL,
`stat_trainer` tinyint(8) NOT NULL,
`stat_class_date` date NOT NULL,
PRIMARY KEY (`stat_id`)

输出将是php while语句的一部分,结果如下:

echo "<table class='list'>";
        echo "<tr>";
        echo "<th>Class Name</th><th>Trainer</th><th>Ave</th><th>Edit</th><th>Delete</th>";
        echo "</tr>";
        while($row = mysql_fetch_assoc($class_result))
        {
            echo "<tr id='class_".$row["id"]."'>"; 
            echo "<td>".$row["class_name"]."</td>";
            echo "<td class='trainer-name'>".$row["trainer_name"]."</td>";
            echo "<td class='trainer-name'>".$row["stat_students_avg"]."</td>";
            echo "<td class='icon'><a href='javascript:void(0);' id='class_edit_".$row["id"]."'><span class='glyphicon glyphicon-edit'></span></a></td>";
            echo "<td class='icon'><a href='javascript:void(0);' id='class_delete_".$row["id"]."'><span class='glyphicon glyphicon-remove'></span></a></td>";
            echo "</tr>";
        }
        echo "</table>";

1 个答案:

答案 0 :(得分:1)

您的ON (id = stat_students_avg)与相同的值不对应。

统计表可以加入类表的唯一方法是类表的 class_trainerid 和统计表的 trainer_id

你可以试试这样的事情

SELECT 
    class_id ,
    class_name,
    class_trainerid,
    class_starttime as start,
    class_ampm as ampm,
    class_days as days,
    trainer_id as trainid,
    trainer_name,
    stat_class as sclass,
    AVG(stat_students) as stat_students_avg
FROM
    $class_table

     LEFT JOIN $trainer_table ON (class_trainerid = trainer_id)
      LEFT JOIN stats ON (trainer_id  = class_trainerid)
GROUP BY
      class_id