例子:
sql两个字段排序
ORDER BY update_time DESC , create_time DESC
<select id="selectLawIllegalActivitiesList" parameterType="LawIllegalActivities" resultMap="LawIllegalActivitiesResult"><include refid="selectLawIllegalActivitiesVo"/><where> <if test="illegalActivities != null and illegalActivities != ''"> and illegal_activities like concat('%', #{illegalActivities}, '%')</if><if test="terms != null and terms != ''"> and terms = #{terms}</if><if test="termsContent != null and termsContent != ''"> and terms_content = #{termsContent}</if><if test="according != null and according != ''"> and according = #{according}</if><if test="accordingContent != null and accordingContent != ''"> and according_content = #{accordingContent}</if></where>ORDER BY update_time DESC , create_time DESC</select>
/*** 查询违法事项库列表* * @param lawIllegalActivities 违法事项库* @return 违法事项库集合*/public List<LawIllegalActivities> selectLawIllegalActivitiesList(LawIllegalActivities lawIllegalActivities);
如果新增数据 只有创建时间 没有更新时间 想要新增数据排到最前面
ORDER BY
COALESCE(update_time, create_time) DESC,
create_time DESC
这里使用了COALESCE函数,它会返回第一个非空参数的值。如果update_time是NULL,则COALESCE(update_time, create_time)将使用create_time的值进行排序。这样,无论是否有更新时间,所有记录都会按照最新的时间戳进行降序排列。
<select id="selectLawIllegalActivitiesList" parameterType="LawIllegalActivities" resultMap="LawIllegalActivitiesResult"><include refid="selectLawIllegalActivitiesVo"/><where> <if test="illegalActivities != null and illegalActivities != ''"> and illegal_activities like concat('%', #{illegalActivities}, '%')</if><if test="terms != null and terms != ''"> and terms = #{terms}</if><if test="termsContent != null and termsContent != ''"> and terms_content = #{termsContent}</if><if test="according != null and according != ''"> and according = #{according}</if><if test="accordingContent != null and accordingContent != ''"> and according_content = #{accordingContent}</if></where>ORDER BYCOALESCE(update_time, create_time) DESC,create_time DESC</select>