简体   繁体   中英

Ordering SQL statement in PHP MYSQL

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?

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') .

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