2

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

  1. [email protected]

    [email protected]
    [email protected]

Now If I make this query

SELECT * FROM email WHERE (email IN ( '[email protected]' ,'[email protected]' ,  '[email protected]' )) AND email!='' LIMIT 3

I get this

[email protected]

[email protected]

[email protected]

but I want to get result on the basis of the arguments in the IN clause If you notice in the IN I mentioned [email protected] first, I dont think order by clause will help me here Any other way of solving this issue?

2
  • The table has enail in this order Do you possibly have a RowID, Or TimeStamp? What do you order it by? Commented Feb 17, 2014 at 19:05
  • @astander it has time stamp and row ID both , but I want to order on the basis of the arguments I supplied in the IN statement. Commented Feb 17, 2014 at 19:06

1 Answer 1

3

One possible approach is using FIELD() MySQL function:

   SELECT * 
     FROM email
    WHERE FIELD(email, '[email protected]', '[email protected]', '[email protected]') <> 0 
 ORDER BY FIELD(email, '[email protected]', '[email protected]', '[email protected]');

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

Sign up to request clarification or add additional context in comments.

6 Comments

@raina660w where is the IN and where clause now?
It was in LIMIT actually, have you checked it?
the updated answer works, what does <> 0 do? I know <> is equal to !=
Updated the answer once again with an explanation. ) In short, FIELD is used both for ordering (obviously) and for filtering out the unwanted strings from the result set (in the previous edition I've done the same with LIMIT 3 clause).
Curious if FIELD will use an index on the email column, or if this amounts to a seq. scan.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.