2016-11-03 49 views
0

我有以下MySQL查詢MySQL的順序按逗號分隔的列表

SELECT * FROM Characters WHERE CharacterID IN (30, 29, 1, 292, 51) 

哪個返回我的人物的名單如下

| CharacterID | CharacterName | 
-------------------------------------- 
|  1  | Character 1 | 
|  29  | Character 29 | 
|  30  | Character 30 | 
|  51  | Character 51 | 
|  292  | Character 292 | 

我需要它在相同的順序返回回來原來的逗號分隔值。因此,在這個特定的實例,我需要它返回......

| CharacterID | CharacterName | 
-------------------------------------- 
|  30  | Character 30 | 
|  29  | Character 29 | 
|  1  | Character 1 | 
|  292  | Character 292 | 
|  51  | Character 51 | 

反正是有,我可以明確說,像

​​

回答

3

您可以通過FIELD()

使用順序
SELECT * FROM Characters WHERE CharacterID IN (30, 29, 1, 292, 51) 
ORDER BY FIELD(CharacterID, 30, 29, 1, 292, 51) 

FIELD()是返回一個逗號分隔的列表

+0

我給它一個鏡頭,感謝的指數位置的功能。顯然我的Google-fu今天關閉了。 –