该题真的是我写SQL遇到的一个需求,整理了一个小问题出给大家做一下,比较固定的办法就是,写今天的日期,代码如下:
select subject.name,T.cnt from
(
select subject_id,count(id) as cnt from submission where `create_time` BETWEEN '2022-02-23' AND '2022-02-24' group by subject_id
) as T join subject
on T.subject_id=subject.id
order by T.cnt desc ,subject.id;
当然我们也可以学习"今天"这个东西的用法,如下:
select subject.name,T.cnt from
(
select subject_id,count(id) as cnt from submission where `create_time` BETWEEN curdate() AND DATE_ADD(curdate(),INTERVAL 1 DAY) group by subject_id
) as T join subject
on T.subject_id=subject.id
order by T.cnt desc ,subject.id;
}
select subject.name,T.cnt from
(
select subject_id,count(id) as cnt from submission where `create_time` BETWEEN curdate() AND DATE_ADD(curdate(),INTERVAL 1 DAY) group by subject_id
) as T join subject
on T.subject_id=subject.id
order by T.cnt desc ,subject.id;