mysql 查询结果如果不给予指定的order by ,那么mysql会按照主键顺序(innodb引擎)对结果集加以排序,那么最后的排序可能就不是你想要的排序结果。
举个例子,我要按照前端传过来的mongoId集合来查询数据库并且返回同样排序的结果集合,如(M3,M5,M4),他们对应的主键顺序为(M4,M3,M5)。如果在查询语句中不加order by 对查询结果排序,那么结果就会变成主键顺序(M4,M3,M5)
就像上图,最后的结果是按VId排的序。那么如果想要按照(M3,M5,M4)的排序返回结果,就需要用到自排序字段 order by field ( value ,str1,str2,str3)
实操案例
SELECT
t1.cjdxzjhm ,
t1.tmz
FROM
t_qyhsjc_GC_check_result t1
WHERE
t1.cjdxzjhm in
(
'340121200111174313',
'341225200308092030',
'510722200109185153',
'340621200011107233',
'342601200110296515'
)
AND t1.tmz='1769A01014612211220155'
增加排序
SELECT
t1.cjdxzjhm ,
t1.tmz
FROM
t_qyhsjc_GC_check_result t1
WHERE
t1.cjdxzjhm in
(
'340121200111174313',
'341225200308092030',
'510722200109185153',
'340621200011107233',
'342601200110296515'
)
AND t1.tmz='1769A01014612211220155'
order by field
(t1.cjdxzjhm,'340121200111174313',
'341225200308092030',
'510722200109185153',
'340621200011107233',
'342601200110296515')
THE END
暂无评论内容