2019独角兽企业重金招聘Python工程师标准>>>
MySQL/sqlserver查询in操作 查询结果按in集合顺序显示
MySQL用下面的语句
select * from ibs6_terminal_adv_inf where id in (16,14,15) order by field(id,16,14,15)
select * from ibs6_terminal_adv_inf where id in (16,14,15) order by find_in_set(id,'16,14,15')
select * from ibs6_terminal_adv_inf where id in (16,14,15) order by substring_index('16,14,15',id,1)
结果就是按顺序显示了
sqlserver 用以下的语句
select * from ibs6_terminal_adv_inf where id in (16,14,15) order by CHARINDEX(','+ltrim(id)+',',',16,14,15,')"
另外还有下面这种case函数方式的
select id from table1 where id in(3224,3225,3395,2810,3280,3281,3292)
order by
case id
when 3224 then 1
when 3225 then 2
when 3395 then 3
when 2810 then 4
when 3280 then 5
when 3281 then 6
when 3292 then 7
end