2017-05-17 19 views
1

查询到从2个表得到的结果:如何写mysql的innerquery时,你得到的记录来自同一个表计数

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, 

而且将R (这不过是如果第一桌counttotalWatchedStudents值是3,这意味着有2种类型的人 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     | 
+0

这看起来很奇怪。您通过“slp.course_id”进行分组,并显示“slp.student_type”和“slp.stu_reference_id”。每门课程不能有不同的学生类型和参考ID?你不在乎你展示的是什么? –

+0

其实对不起,我不需要那列'student_type'它是为了哪里的条件。不会只有一种学生类型会出现,因为如果您看到我们只调用了''student_type ='institue''和'stu_reference_id = 34'的条件。 –

+0

啊,你说得对;我忽略了这一点。 –

回答

2

如果我理解正确的话,你要总结其在另一列看见超过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 

希望这可以帮助

+0

你可以请检查我的更新表最后一列我想要的结果,它只不过是总和,但总和我们正在让学生数,然后我们如何才能得到watched_pa​​ercentage总和只有选定的学生......? –

+1

我改变了我的查询,并添加了'fullwatchedStudentsSum' –

+0

@它正在工作,但请您解释行为..? –

2

所以你想要计算学生与watched_percentage >= 90?使用条件汇聚了这一点:

SELECT 
    tbc.course_name, 
    slp.course_id, 
    slp.student_type, 
    slp.stu_reference_id, 
    COUNT(*) as counttotalWatchedStudents, 
    SUM(slp.watched_percentage >= 90) as fullwatchedStudentsCount, 
    SUM(slp.watched_percentage < 90) as stillwatchedStudentsCount 
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; 

MySQL的对待真= 1,假= 0,这样你就可以简单地总结了trues :-)

+1

不错的解决方案,不知道你可以省略if语句:) –

+2

是的,但除此之外,我的答案与你的答案相同。当我发布我的时候,我没有看到你的。 –

+0

@JanZeiseweis你可以请检查我的更新表最后一列我想要的结果,它只不过是总和,但总和我们正在让学生数,然后我们如何才能得到watched_pa​​ercentage总和只有选定的学生......? –

相关问题