简体   繁体   中英

mysql get certain rows in order

How can I get certain rows from a table in order? For Example I have the following rows:

EmpID   Name
81      Albert
22      Ashley
71      John
42      Jack
55      Bob

Primary Key is EmpID. I want to get only: John, Albert, Bob IN ORDER. Result should be:

71      John
81      Albert
55      Bob

How can I do that using MySQL query? Thanks heaps !

Try this one, it use custom sort

SELECT *
FROM TABLENAME
WHERE EmpID IN (72,81,55)
ORDER BY FIELD(NAME, 'John', 'Albert', 'Bob')

Put this at the end of your SQL statement:

ORDER BY CASE EmpID
    WHEN 71 THEN 1  
    WHEN 81 THEN 2  
    WHEN 55 THEN 3  
    ELSE 4
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM