有一张通知(notification)表;
需求:
1.状态(status)为发布“on”要排在未发布“off”前面;
2.先按发布时间排序再按创建时间排序,发布时间为空的要排在后面。
select * from notification;
| id | content | status | create_time | publiish_time |
| 1 | aaa | on | 2018-10-17 12:45:00 | 2018-10-17 12:45:00 |
| 2 | bbb | off | 2018-10-17 12:45:00 | |
| 3 | ccc | on | 2018-10-17 12:45:00 | 2018-10-17 12:45:00 |
| 4 | ddd | on | 2018-10-17 12:45:00 | 2018-10-17 12:45:00 |
| 5 | eee | off | 2018-10-17 12:45:00 |
<select id="getResultList" parameterType="com.hekliu.NotificationQry" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM notification
WHERE
1 = 1
<if test="content != null and content !=''">
AND content = #{content}
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="startPublishDate != null and startPublishDate != ''">
AND <![CDATA[DATE_FORMAT(publish_time,'%Y-%m-%d %H:%i:%s') >= #{startPublishDate}]]>
</if>
<if test="endPublishDate != null and endPublishDate != ''">
AND <![CDATA[DATE_FORMAT(publish_time,'%Y-%m-%d %H:%i:%s') <= #{endPublishDate}]]>
</if>
ORDER BY
status = "off", status = "on", create_time DESC, publish_time is NULL, publish_time DESC
</select>
原文链接:https://blog.youkuaiyun.com/liu59412/article/details/83141566
本文详细介绍了一种使用MyBatis进行复杂排序和过滤的方法,包括如何通过动态SQL实现基于状态和其他条件的排序,以及如何处理发布时间为空的情况。具体展示了在通知表中,如何将已发布的通知排在未发布的前面,并按发布时间和创建时间进行排序。
446

被折叠的 条评论
为什么被折叠?



