Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何按顺序从表中获取某些行?例如,我有以下行:
EmpID Name 81 Albert 22 Ashley 71 John 42 Jack 55 Bob
主键是 EmpID。我只想得到:John、Albert、Bob IN ORDER。结果应该是:
71 John 81 Albert 55 Bob
如何使用 MySQL 查询来做到这一点?多谢!
试试这个,它使用自定义排序
SELECT * FROM TABLENAME WHERE EmpID IN (72,81,55) ORDER BY FIELD(NAME, 'John', 'Albert', 'Bob')
将其放在 SQL 语句的末尾:
ORDER BY CASE EmpID WHEN 71 THEN 1 WHEN 81 THEN 2 WHEN 55 THEN 3 ELSE 4 END