我的SQL查询需要返回一个值列表以及日期,但凭借我有限的知识,我只能做到这一点。
这是我的SQL:
select lsu_students.student_grouping,lsu_attendance.class_date,
count(lsu_attendance.attendance_status) AS count
from lsu_attendance
inner join lsu_students
ON lsu_students.student_grouping="Central1A"
and lsu_students.student_id=lsu_attendance.student_id
where lsu_attendance.attendance_status="Present"
and lsu_attendance.class_date="2015-02-09";
返回:
student_grouping class_date count
Central1A 2015-02-09 23
我希望它返回:
student_grouping class_date count
Central1A 2015-02-09 23
Central1A 2015-02-10 11
Central1A 2015-02-11 21
Central1A 2015-02-12 25
此查询根据学生分组获取日期列表:
select distinct(class_date)from lsu_attendance,lsu_students
where lsu_students.student_grouping like "Central1A"
and lsu_students.student_id = lsu_attendance.student_id
order by class_date
答案 0 :(得分:2)
我想你只想要group by
:
select s.student_grouping, a.class_date, count(a.attendance_status) AS count
from lsu_attendance a inner join
lsu_students s
ON s.student_grouping = 'Central1A' and
s.student_id = a.student_id
where a.attendance_status = 'Present'
group by s.student_grouping, a.class_date;
评论:
where
。group by
。除MySQL之外的任何SQL引擎都需要这样做。答案 1 :(得分:1)
只需取出and lsu_attendance.class_date="2015-02-09"
或将其更改为某个范围,然后添加(最后)GROUP BY lsu_students.student_grouping,lsu_attendance.class_date
。