Home > Database > Mysql Tutorial > mysql in查询结果顺序

mysql in查询结果顺序

WBOY
Release: 2016-06-07 15:33:26
Original
1224 people have browsed it

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); 

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template