MySQL:过去12个月中由老师分组的课程

时间:2018-03-07 14:44:12

标签: mysql

我在在线学费中心有2个教师和课程表,如下所示:

教师表:

+----+--------------------+-----------+----------+
| id | booking_time       | teacher   | student  |
+----+--------------------+-----------+----------+
| 1  | 2017-08-08 12:00:00| 3         | 40       |
| 2  | 2017-10-03 12:00:00| 1         | 50       |
| 3  | 2017-12-01 12:00:00| 2         | 60       |
| 4  | 2018-01-01 12:00:00| 3         | 40       |
| 5  | 2018-02-01 12:00:00| 2         | 60       |
| 6  | 2018-03-02 12:00:00| 3         | 40       |
| 7  | 2018-03-04 12:00:00| 1         | 50       |
| 8  | 2018-03-06 12:00:00| 1         | 50       |
+----+--------------------+-----------+----------+

课程表:

+-------+------+------+------+------+------+------+------+------+------+-------+------+------+
|Teacher|Mar-18|Feb-18|Jan-18|Dec-17|Nov-17|Oct-17|Sep-17|Aug-17|Jul-17| Jun-17|May-17|Apr-17|
+-------+------+------+------+------+------+------+------+------+------+-------+------+------+
|John   |2     |0     |0     |0     |0     |1     |0     |0     |0     |0      |0     |0     |
|Bill   |1     |0     |1     |0     |0     |0     |0     |1     |0     |0      |0     |0     |
|Mike   |0     |1     |0     |1     |0     |0     |0     |0     |0     |0      |0     |0     | 
+-------+------+------+------+------+------+------+------+------+------+-------+------+------+

我需要在过去12个月(从当月开始)获得每位教师的预订课程,并按当前月份的最高预订进行排序。类似的东西:

select value from mytable where 
indx = @var
and f() in (select num from nums);

任何帮助我怎么能这样做?

0 个答案:

没有答案