SELECT tmp.user_id, tmp.course_id, tmp.type, tmp.expire_time, @rownum := @rownum + 1, IF ( @course_id = tmp.course_id ,@rank := @rank + 1, @rank := 1 ) AS rank, @course_id := tmp.course_id FROM ( SELECT user_id, course_id, type, add_time, expire_time FROM xz_user_course WHERE user_id = 296154 ORDER BY course_id ASC, expire_time DESC ) tmp, ( SELECT @rownum := 0, @course_id := 0, @rank := 0 ) a
执行结果如图:
备注:此次还有rownum的自动增长标注哦