繁体   English   中英

带AVG()的嵌套SQL语句

[英]Nested SQL statement with AVG()

我有三个要提取数据的表。 一个是班级表,其中存储了所提供的班级列表,包括ID,时间戳,培训师ID,开始时间,上午/下午以及举办课程的日期。 培训师ID是一个外键,将其绑定到培训师表,在该表中,我根据培训师的唯一ID拉出培训师名称。

效果很好,但我还需要显示每个班级的平均出勤率。 这涉及一个统计信息表,该表具有一个类ID外键,该类将该类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_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>";

您的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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM