Maison  >  Article  >  base de données  >  mysql in查询结果顺序

mysql in查询结果顺序

WBOY
WBOYoriginal
2016-06-07 15:33:261170parcourir

MySQL 查询in操作,查询结果按in集合顺序显示 : select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5'); 偶尔看到的。。。或许有人会注意过,但我以前真不知道 SQL: select * from table where id IN (3,6,9,1,2,5,8,7); 这样的情况取出

MySQL 查询in操作,查询结果按in集合顺序显示 :

select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5'); 

偶尔看到的。。。或许有人会注意过,但我以前真不知道 
SQL: select * from table where id IN (3,6,9,1,2,5,8,7); 

这样的情况取出来后,其实,id还是按1,2,3,4,5,6,7,8,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能完成?是否需要取回来后再foreach一下?其实mysql就有这个方法 

sql: select * from table where id IN (3,6,9,1,2,5,8,7) order by field(id,3,6,9,1,2,5,8,7); 

出来的顺序就是指定的顺序了

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn