How to select strings using the IN operator in exactly the order in which they are transferred? Those. SELECT id, name FROM users WHERE id IN (2, 3, 1)
1 answer
You can use the FIELD() function, passing it the exact same sequence that you specify in IN . The function will return the index of the value in the sequence and can be sorted by the ORDER BY .
SELECT id, name FROM users WHERE id IN (2, 3, 1) ORDER BY FIELD(id, 2, 3, 1) - Please describe in steps how this will work in SQL Server. - Vladimir Smirnov
- @ Vladimir Smirnov Unfortunately, FIELD is not a standard function and will work only in MySQL and DBMS where it is implemented. I'm afraid for MS SQL will have to pick up an alternative solution. - cheops
- Yes, it works, thanks! Need to remember. - Anton
|