我有以下查询:

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

最佳答案

您只需要使用GROUP BY:

SELECT semester_id, COUNT(*) as count
 FROM marks
 WHERE student_id=150
  AND semester_id<=1914
 GROUP BY semester_id;

09-26 22:45