avoid Sorting by the MYSQL IN Keyword

后端 未结 4 507
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 14:34

When querying the db for a set of ids, mysql doesnot provide the results in the order by which the ids were specified. The query i am using is the following:



        
4条回答
  •  伪装坚强ぢ
    2020-12-19 15:16

    If you want to order your result by id in the order specified in the in clause you can make use of FIND_IN_SET as:

    SELECT id ,title, date 
    FROM Table 
    WHERE id in (7,1,5,9,3)
    ORDER BY FIND_IN_SET(id,'7,1,5,9,3')
    

提交回复
热议问题