我有以下查询:
select exam_id,semester_id from marks where student_id=150 and semester_id<=1914;
它的输出是:
exam_id | semester_id
27 1913
68 1913
64 1914
我想找出类似semester_id
的计数。
当我尝试mysql_num_rows
时,它会显示3.
但我想要这样的输出
semester_id 1913---------->count 2
semester_id 1914---------->count 1
答案 0 :(得分:6)
您只需使用GROUP BY
:
SELECT semester_id, COUNT(*) as count
FROM marks
WHERE student_id=150
AND semester_id<=1914
GROUP BY semester_id;
答案 1 :(得分:3)
试试这个
select semester_id,count(*) as cnt
from marks
where student_id=150 and semester_id<=1914
group by semester_id;
答案 2 :(得分:3)
使用Count()
聚合函数即可实现此目的。
SELECT semester_id, COUNT(exam_id) FROM marks
WHERE student_id=150 AND semester_id<=1914
GROUP BY semester_id;
答案 3 :(得分:1)
Select semester_id, count(exam_id)
From marks
Group By semester_id;
如果您有需要,请使用:
count(distinct exam_id)
答案 4 :(得分:1)
使用count()
SELECT semester_id, COUNT(*) as count
FROM marks
WHERE student_id=150
AND semester_id<=1914
GROUP BY semester_id;