MySQL specify arbitrary order by id

前端 未结 3 2012
一向
一向 2020-12-17 18:49

Is it possible to specify an arbitrary order for a MySQL SELECT statement? E.g.,

SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY (         


        
3条回答
  •  太阳男子
    2020-12-17 18:49

    FIND_IN_SET function will do the trick

    SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(id, '1,3,2,9,7');
    

    http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

    EDIT: Note the lack of spaces in the string argument of the find_in_set function.

提交回复
热议问题