0

I am trying to run a query off of my mysql database using the IN operator.

SELECT usernum FROM usergroup WHERE emailmain IN ('[email protected]', '[email protected]', '[email protected]')

The query works but instead of outputing the usernum values in the order entered it outputs them in descending order. This is problematic because I need them to be aligned to the emails. I think it may have to do with the limit operator or by using an ORDER BY emailmain. I've tried that and it still doesn't work because assuming the email doesn't exist in the database it won't correspond to the memory location in the array.

The most important thing to me is that I know the emails that aren't in the database.

Any help would be appreciated!

6
  • 1
    Why would you expect them in any order if you do not specify one? It may help to use the "EXPLAIN SELECT ..." syntax to decided on which index the database is using to return your data. If you do not specify one - mysql will use the index that it decided was best to answer your query Jan 5, 2012 at 4:50
  • Yea i'm not sure what I was thinking. Its late and I didn't think about indexes. However, I am still running into the issue of it not telling me which emails aren't in the database.
    – Trent
    Jan 5, 2012 at 5:03
  • 1
    this could be your problem - you are thinking like I do - which is as a programmer - not DBA - 99.9999% of all emails are not in you db - you need to think "set theory" on what you want - a rough guess is "this list of emails is not in my DB" My suggestion is to create a table of 'said' email and then do a left join and check the NULLs. Jan 5, 2012 at 5:06
  • 1
    That is a really good idea Adrian. I wish I didn't have to create a table each time but that will work. I really appreciate the help. I wish I could give you some more rep. Thanks again!
    – Trent
    Jan 5, 2012 at 5:13
  • 1
    if I got paid by rep I'd care :-) but hopefully you've worked out a good solution for your self Jan 5, 2012 at 5:20

5 Answers 5

1

If you are creating the IN string dynamically, you don't have to create a table every time you run the query - you can use an inline view, populated in the same way as the IN string, like so:

select v.email, u.usernum
from 
(select '[email protected]' email union select '[email protected]' union select '[email protected]') v
left join usergroup u on v.email = u.emailmain
order by v.email
1
  • I had some issues with this at first but it works perfectly by just changing the collation in phpmyadmin to latin1_swedish_ci. I really appreciate the help. Thanks!
    – Trent
    Jan 5, 2012 at 17:13
1

You can use ORDER BY FIELD to define your order

SELECT usernum 
FROM usergroup 
WHERE emailmain IN ('[email protected]', '[email protected]', '[email protected]')
ORDER BY FIELD (emailmain, '[email protected]', '[email protected]', '[email protected]')
3
  • I tried this but it wouldn't show me the emails not in the database. Thank you for the help though!
    – Trent
    Jan 5, 2012 at 4:59
  • This isn't meant to show you the emails in the database. This will only order the emails in the exact order that you specify.
    – JohnP
    Jan 5, 2012 at 5:13
  • This didn't solve the issue but it will definitely be useful in the future! Thank you!
    – Trent
    Jan 5, 2012 at 5:14
1

IN won't impact the order of your results at all, without an ORDER clause rows will come back in whatever order they're found in.

If you need to match the order of the results to the order of your supplied email list, try this:

SELECT usernum
  FROM usergroup
  WHERE emailmain IN ('[email protected]', '[email protected]', '[email protected]')
  ORDER BY FIND_IN_SET(emailmain, "[email protected],[email protected],[email protected]")

FIND_IN_SET will return the position of the given string in a comma-delimited list

With larger lists it would probably be safer though to just use SELECT usernum, emailmain FROM ... to return both fields in each result and correlate them application-side.

3
  • 1
    I didn't know about the FIELD function JohnP posted while I was working on my answer. It's probably a better choice than FIND_IN_SET Jan 5, 2012 at 4:57
  • Neither did I. It will be helpful in the future. However, the main reason why its important I get it sorted correctly is so that I know which emails are not in the database. After executing i'm using mysql_fetch_assoc to get the values and then set them equal to an array. However, it won't show the emails that aren't in the database.
    – Trent
    Jan 5, 2012 at 5:06
  • Maybe what you want is SELECT usernum FROM usergroup WHERE emailmain NOT IN ('[email protected]', '[email protected]', '[email protected]') Jan 5, 2012 at 18:46
0

any reason why you're not just selecting the username and email from the database?

SELECT usernum, emailmain FROM usergroup WHERE emailmain IN ('[email protected]', '[email protected]', '[email protected]') ORDER BY emailmain DESC
0

Providing IN condition does not imply the order.

To order your result, use ORDER BY at the end of your query.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.