例如,我在表EMPLOYEE中具有:
(code, name) (1, 'Jimmy') (2, 'Albert') (3, 'Michelle') (4, 'Felix' )
如果您这样做:(从EMPLOYEE中选择*),您将获得:
(1, 'Jimmy') (2, 'Albert') (3, 'Michelle') (4, 'Felix' )
如果您这样做:(从EMPLOYEE中选择*,其中(1,3,2,4)中的代码将得到:
如何按原样在IN子句中按CSV值的顺序获取它?
(1, 'Jimmy') (3, 'Michelle') (2, 'Albert') (4, 'Felix' )
使用FIND_IN_SET函数:
SELECT e.* FROM EMPLOYEE e WHERE e.code in (1,3,2,4) ORDER BY FIND_IN_SET(e.code, '1,3,2,4')
或使用CASE语句:
SELECT e.* FROM EMPLOYEE e WHERE e.code in (1,3,2,4) ORDER BY CASE e.code WHEN 1 THEN 1 WHEN 3 THEN 2 WHEN 2 THEN 3 WHEN 4 THEN 4 END