问题
现在一个需求是查询某一列,用逗号分开,返回的结果要根据输入的顺序返回结果
比如:姓名的输入框输入的是(zhangsan,lisi),那么返回的结果也要是按照(zhangsan,lisi)这样的顺序展示
测试
有如下表classroom,内容如下
如果根据字段名称去查,那么它会根据字典顺序排序,如下所示
select * from classroom where classname in ("class2","class3") order by classname
如果想根据我in里面的顺序去排序,那么只能是如下所示
select * from classroom where classname in ("class2","class3") order by field(classname,"class3","class2")
如果我想在原来的基础上,在根据时间排序
select * from classroom where classname in ("class2","class3") order by field(classname,"class3","class2") ,createTime
注意:
如上面的SQL所示,by field里的 条件必须比 in 里面的查询条件多,如果少一个,那么这个排序就不会成功
//成功 select * from classroom where classname in ("class2","class3") order by field(classname,"class3","class1","class2") //失败,少class2 select * from classroom where classname in ("class2","class3") order by field(classname,"class3","class1") //失败,少class3 select * from classroom where classname in ("class2","class3") order by field(classname,"class1","class2")