MySQL 常用语法MyBatis

MySQL 使用 order by field 自定义排序

2021-12-23  本文已影响0人  思念_似水流年
SELECT * FROM project_case where
case_id in (119, 113, 117)
order by case_id asc;

SELECT * FROM luckyframe.project_case where
case_id in (119, 113, 117)
order by case_id desc;
SELECT * FROM project_case where
case_id in (119, 113, 117)
order by field(case_id, 119, 113, 117);

返回结果按照指定的字段和指定的顺序进行排序:


指定顺序排序
MyBatis 使用 order by field 语法
<select id="selectSortedProjectCaseByIds" parameterType="String" resultMap="ProjectCaseResult">
        SELECT * FROM project_case where
        case_id in
        <foreach collection="caseIdList" item="caseId" open="(" close=")" separator=",">
            #{caseId}
        </foreach>
        order by field
        <foreach collection="caseIdList" item="caseId" open="(case_id," close=")" separator=",">
            #{caseId}
        </foreach>
    </select>

Mapper.java 中调用的代码如下:

List<ProjectCase> selectSortedProjectCaseByIds(@Param("caseIdList") List<Integer> caseIdList);

传递的 caseIdList 是已经排好序的列表。

上一篇下一篇

猜你喜欢

热点阅读