select * from `table` where `id` in (3,2,1,5) order by INSTR(',3,2,1,5,',concat(',',`id`,','))最近问这个问题的怎么这么多,这是本人在CSDN首创的一种方法。 [align=center]====思想重于技巧==== [/align]
select * from `table` where `id` in (3,2,1,5)===》 这样就不用ORDER BY ,而且还很好的用到了索引。select * from `table` where `id` =3 union all select * from `table` where `id` =2 union all select * from `table` where `id` =1 union all select * from `table` where `id` =5
最简单的.不过没有我LS写的效率高. select * from `table` where find_in_set(`id`,'3,2,1,5') order by find_in_set(`id`,'3,2,1,5')
select * from `table` where id in (3,2,1,5) order by if(id=3,1,if(id=2,2,if(id=1,3,4))) 不推荐在ORDER BY 中用字符串函数,因为速度慢
where `id` in (3,2,1,5)
order by INSTR(',3,2,1,5,',concat(',',`id`,','))最近问这个问题的怎么这么多,这是本人在CSDN首创的一种方法。
[align=center]==== 思想重于技巧 ====
[/align]
这样就不用ORDER BY ,而且还很好的用到了索引。select * from `table` where `id` =3
union all
select * from `table` where `id` =2
union all
select * from `table` where `id` =1
union all
select * from `table` where `id` =5
select * from `table` where find_in_set(`id`,'3,2,1,5') order by find_in_set(`id`,'3,2,1,5')
if(id=3,1,if(id=2,2,if(id=1,3,4)))
不推荐在ORDER BY 中用字符串函数,因为速度慢