SQL查询排序OrderBy

1.我要查询课程排序,有三种状态:正在上课、即将上课、已结束
2.大排序按,正在上课、即将上课、已结束
3.小排序,正在上课、即将上课(按时间正序),已结束(按时间倒序)

如何在一个SQL中实现

阅读 2.4k
2 个回答

正在上课、即将上课、已结束 对应status 1,2,3

select *from table order by status,case status when 1 then sorttime end asc,case status when 2 then sorttime end asc,case status when 3 then sorttime end desc;

select status, time ,(case when status = '1' then time when status = '2' -time end) sorttime from class order by status,sorttime
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题