sql按照in中的顺序进行排序 mysql

经测试以下三种情况,都可以。

SELECT a.id,a.name as goods_name,a.logoimg,b.name as store_name FROM sh_goods a LEFT JOIN sh_store b on a.store_id = b.id WHERE ( a.id IN ('1127','1124','1128','1130','1132','1134') ) order by find_in_set(a.id,'1127,1124,1128,1130,1132,1134');
SELECT a.id,a.name as goods_name,a.logoimg,b.name as store_name FROM sh_goods a LEFT JOIN sh_store b on a.store_id = b.id WHERE ( a.id IN ('1127','1124','1128','1130','1132','1134') ) order by field(a.id,1127,1124,1128,1130,1132,1134);
SELECT a.id,a.name as goods_name,a.logoimg,b.name as store_name FROM sh_goods a LEFT JOIN sh_store b on a.store_id = b.id WHERE ( a.id IN ('1127','1124','1128','1130','1132','1134') ) order by substring_index('1127,1124,1128,1130,1132,1134',a.id,1);

posted @ 2016-03-23 14:14  TBHacker  阅读(1357)  评论(0编辑  收藏  举报