Maintaining order in MySQL “IN” query

后端 未结 2 757
南笙
南笙 2020-11-28 05:41

I have the following table

DROP TABLE IF EXISTS `test`.`foo`;
CREATE TABLE  `test`.`foo` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(         


        
2条回答
  •  独厮守ぢ
    2020-11-28 06:28

    As the other answer mentions: the query you posted has nothing about what order you'd like your results, just which results you'd like to get.

    To order your results, I would use ORDER BY FIELD():

    SELECT * FROM foo f where f.id IN (2, 3, 1)
    ORDER BY FIELD(f.id, 2, 3, 1);
    

    The argument list to FIELD can be variable length.

提交回复
热议问题