Mysql Count:只显示一个结果

时间:2016-01-31 08:14:24

标签: mysql sql select count

我想知道在MySQL数据库中计算数据的正确查询是什么。

例如,我想计算有缺席注释的数据。所以结果应该是。

|stud_name    |total_absent|
|John Bandola |13          |
|Jeff Oliveras|10          |
|Wendy Lizardo|5           |

等。等

但问题是,它只显示1个结果,就像这样

|stud_name    |total_absent|
|John Bandola |13          |

这是我的查询

SELECT 
    stud_name, 
    COUNT(remark) as total_absent 
FROM tb_attendance 
WHERE instructor_id = 'INST-20131296' 
    AND subj_code = 'C100' 
    AND description = 'ADBMS' 
    AND remark = 'Absent'

3 个答案:

答案 0 :(得分:0)

由于您需要按stud_name汇总结果,因此您需要group by该字段:

SELECT 
    stud_name, 
    COUNT(remark) as total_absent 
FROM tb_attendance 
WHERE instructor_id = 'INST-20131296' 
    AND subj_code = 'C100' 
    AND description = 'ADBMS' 
    AND remark = 'Absent'
GROUP BY `stud_name` -- here

答案 1 :(得分:0)

SQL GROUP BY子句与SELECT语句协作使用,将相同的数据排列成组。当您谈论集合时,

SELECT 
    stud_name, 
    COUNT(remark) as total_absent 
FROM tb_attendance 
WHERE instructor_id = 'INST-20131296' 
    AND subj_code = 'C100' 
    AND description = 'ADBMS' 
    AND remark = 'Absent' Group By stud_name;

答案 2 :(得分:0)

从tb_attendance中选择count(*),其中remark ='缺席'