简体   繁体   中英

Get result in given order in mysql

i have a table with 2 columns like,

id  |   name
------------
1   |   abc
2   |   efg
3   |   kkk
4   |   lop
5   |   xyz

Query:

select id from name where name IN ('kkk','lop','xyz','kkk','efg');

given result:

2   |   efg
3   |   kkk
4   |   lop
5   |   xyz

expected result:

id  |   name
-------------   

3   |   kkk
4   |   lop
5   |   xyz
3   |   kkk
2   |   efg

Any ideas?

You can sort in this way:

But you can not duplicate with single query.

select id from name where name IN ('kkk','lop','xyz','kkk','efg') ORDER BY
FIELD(name,'kkk','lop','xyz','kkk','efg'); 

Unless you have to write union query to get your expected results.

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