按照距离当前时间最近并且返回带有序号的sql语句

mysql 考试管理,考试有不同的状态,首先根据不同的状态排序,在根据不同 状态下根据距离当前时间最近的排序

 <select id="getSelectedIndex" resultMap="resultMap">
        SELECT *  FROM ( SELECT *  FROM ( select (@rank:=@rank+1) AS row_number , page_test.* FROM ( select
        <include refid="columnSql"></include>
        from exp_examine t
        <where>
            <if test="id != null  ">
                and t.ID = #{id}
            </if>
            <if test="name != null and name != ''  ">
                and t.NAME LIKE "%"#{name}"%"
            </if>
            <if test="startTime != null  ">
                and t.START_TIME = #{startTime}
            </if>
            <if test="endTime != null  ">
                and t.END_TIME = #{endTime}
            </if>
            <if test="subId != null  ">
                and t.SUB_ID = #{subId}
            </if>
            <if test="examId != null  ">
                and t.EXAM_ID = #{examId}
            </if>
            <if test="isDelete != null  ">
                and t.IS_DELETE = #{isDelete}
            </if>
            <if test="createTime != null  ">
                and t.CREATE_TIME = #{createTime}
            </if>
            <if test="createUser != null  ">
                and t.CREATE_USER = #{createUser}
            </if>
            <if test="status != null and status != ''  ">
                and t.STATUS = #{status}
            </if>
            <if test="remarkOnSpot != null  ">
                and t.REMARK_ON_SPOT = #{remarkOnSpot}
            </if>
        </where>

        ) page_test ,(SELECT @rank:=0) tt
        )  a ORDER BY CASE WHEN STATUS IS NULL THEN 1 WHEN STATUS = '10U' THEN 1 WHEN STATUS = '10R' THEN 1 ELSE 0 END DESC,
        abs(TIMESTAMPDIFF(SECOND,now(),START_TIME)) asc ) b


        <if test="page != null and page.orderBy != null and page.orderBy != ''">
            order by ${page.orderBy}
        </if>

    </select>

猜你喜欢

转载自blog.csdn.net/weixin_38289196/article/details/81128766
今日推荐