Question

I have a table called email from which I fetch email. The table has email in this order

  1. asda@gmaila.omc

    123@dsad.com
    name@gmail.com

Now If I make this query

SELECT * FROM email WHERE (email IN ( 'asda@gmaila.omc' ,'123@dsad.com' ,  'name@gmail.com' )) AND email!='' LIMIT 3

I get this

123@dsad.com

asda@gmaila.omc

name@gmail.com

but I want to get result on the basis of the arguments in the IN clause If you notice in the IN I mentioned asda@gmaila.omc first, I dont think order by clause will help me here Any other way of solving this issue?

Was it helpful?

Solution

One possible approach is using FIELD() MySQL function:

   SELECT * 
     FROM email
    WHERE FIELD(email, 'asda@gmaila.omc', '123@dsad.com', 'name@gmail.com') <> 0 
 ORDER BY FIELD(email, 'asda@gmaila.omc', '123@dsad.com', 'name@gmail.com');

As FIELD returns 0 if its first argument isn't equal to any other, FIELD(str, 'aaa', 'bbb'...) <> 0 clause is roughly equivalent to str IN ('aaa', 'bbb').

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top