0

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?

diiN__________
  • 7,393
  • 6
  • 42
  • 69
xoreax
  • 1
  • 1
    Possible duplicate of [Force MySQL to return duplicates from WHERE IN clause without using JOIN/UNION?](http://stackoverflow.com/questions/2259160/force-mysql-to-return-duplicates-from-where-in-clause-without-using-join-union) – diiN__________ Nov 16 '16 at 08:53
  • Possible duplicate of [MySQL - ORDER BY values within IN()](http://stackoverflow.com/questions/958627/mysql-order-by-values-within-in) – secelite Nov 16 '16 at 09:13
  • thanks diiN_ and secelite – xoreax Nov 16 '16 at 09:31

1 Answers1

0

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.

Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25