注:标准sql语法 oracle通用
场景:参数为List<实体类> list ,
list(0) : busLv = “aa” seatLv=“xxxxx”
list(1) : busLv = “xxx” seatLv=“xxx”
list(2) : busLv = “bb” seatLv =“xxxx”
需要 把查询结果按照传入的list参数顺序进行排序
SELECT s.* FROM s_bus_type s
WHERE (s.BUS_LV = '001003' and s.SEAT_LV = '002003')
or (s.BUS_LV = '001001' and s.SEAT_LV = '002001')
or (s.BUS_LV = '001002' and s.SEAT_LV = '002002')
ORDER BY
case when s.BUS_LV = '001003' and s.SEAT_LV = '002003' then 0
when s.BUS_LV = '001001' and s.SEAT_LV = '002001' then 1
when s.BUS_LV = '001002' and s.SEAT_LV = '002002' then 2
end asc
在mybatis 中 用foreach即可
注意误区:
1、不能用order by busLv,seatLv 这个不解释了
2、 不能用order by field(字段,list .foreach) 该情况只适用于单参数
3、 不能使用动态变量的形式 ,例如
SELECT @mark := @mark + 1 AS mark,
s.*
FROM
(SELECT @mark := 0) t ,
s_bus_type s
WHERE (s.BUS_LV = '001003' and s.SEAT_LV = '002003')
or (s.BUS_LV = '001001' and s.SEAT_LV = '002001')
or (s.BUS_LV = '001002' and s.SEAT_LV = '002002')
ORDER BY mark
如图: 查询并不按or条件顺序