mysql - 从同一张表中获取记录数时如何编写mysql innerquery

标签 mysql join count inner-join average

从 2 个表中获取结果的查询:

SELECT path.* FROM (SELECT  tbc.course_name
slp.course_id,slp.student_type,slp.stu_reference_id,
count(slp.course_id) as counttotalWatchedStudents
 from tbl_student_learning_path  slp LEFT JOIN tbl_courses tbc
 on tbc.course_pid = slp.course_id WHERE slp.stu_reference_id =34 
and slp.student_type='institute' GROUP BY slp.course_id ) as path 

查询结果表:

| course_id                     | totalCollegeStudents | fullwatchedStudentsCount | counttotalWatchedStudents | sumOfWatchPointsForWatchedStudents | totalStudentsAvg | fullwatchedAvg |
|-------------------------------|----------------------|--------------------------|---------------------------|------------------------------------|------------------|----------------|
| Number Systems                | 9                    | 0                        | 3                         | 60                                 | 20.0000          | 0              |
| Percentages                   | 9                    | 0                        | 3                         | 30                                 | 10.0000          | 0              |
| Blood Relations               | 9                    | 3                        | 3                         | 300                                | 100.0000         | 300            |
| Calandar                      | 9                    | 3                        | 3                         | 300                                | 100.0000         | 300            |
| Percentages                   | 9                    | 3                        | 3                         | 300                                | 100.0000         | 300            |
| Permutation & Combination | 9                    | 3                        | 3                         | 300                                | 100.0000         | 300            |
| Probability                   | 9                    | 0                        | 3                         | 90                                 | 30.0000          | 0              |
| Ratios                        | 9                    | 0                        | 3                         | 120                                | 40.0000          | 0              |
| Time and Work                 | 9                    | 0                        | 3                         | 150                                | 50.0000          | 0              |
| Time Speed & Distance     | 9                    | 1                        | 3                         | 140                                | 46.6667          | 100            |
| Averages                      | 9                    | 3                        | 3                         | 300                                | 100.0000         | 300            |
| Coding and Decoding           | 9                    | 3                        | 3                         | 300                                | 100.0000         | 300            |

从上表中,我想在主查询中添加内部查询 查询应该是这样的:

( select count(t1.watched_percentage) from tbl_student_learning_path t1 
WHERE t1.stu_reference_id =34 and t1.student_type='institute' 
AND t1.watched_percentage >= 90 group by t1.course_id  )
fullwatchedStudentsCount,

结果应该是这样的(这没什么,但是如果第一个表 counttotalWatchedStudents 的值为 3,则意味着有两种类型的人 1.学生观看满(watched_percentage>=90) 2.仍在观看的学生(watched_percentage 1-89) )

| fullwatchedStudentsCount | fullwatchedStudentsSum |
|--------------------------|------------------------|
| 2                        | 200                    |
| 1                        | 100                    |
| 0                        | 0                      |
| 2                        | 200                    |
| 1                        | 100                    |
| 1                        | 100                    |
| 0                        | 0                      |
| 2                        | 200                    |
| 2                        | 200                    |
| 1                        | 100                    |
| 2                        | 200                    |
| 1                        | 100                    |

最佳答案

如果我没理解错的话,你想把看到90%以上的学生汇总到另一列:

我会这样做:

SELECT tbc.course_name,
       slp.course_id,
       slp.student_type,
       slp.stu_reference_id,
       count(slp.course_id) AS counttotalWatchedStudents,
       sum(if(slp.watched_percentage>=90, 1, 0)) AS fullwatchedStudentsCount
       sum(if(slp.watched_percentage>=90, watched_percentage, 0)) AS fullwatchedStudentsSum
FROM tbl_student_learning_path slp
LEFT JOIN tbl_courses tbc ON tbc.course_pid = slp.course_id
WHERE slp.stu_reference_id =34
    AND slp.student_type='institute'
GROUP BY slp.course_id
enter code here

希望对你有帮助

关于mysql - 从同一张表中获取记录数时如何编写mysql innerquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44022776/

相关文章:

MySQL SELECT DISTINCT 与多个 JOINS

SQL 计数列和列表值计数

C# 计算列表中的出现次数

php - 如何拆分 ActiveRecord 查询以提高速度

java - 在 NetBeans 中设置类路径

R 如何按条件连接data.table?

MySQL SELECT 列 COUNT(name) WHERE 条件 GROUP BY 列?

mysql - INSERT ... SELECT 语法不会插入所有行

php - MySql LEFT 连接多个具有相同 ID 或名称的表

mysql - 你如何基于一列连接两个表,除非它为空,在这种情况下,基于两个不同的列连接