简体   繁体   English

mysql IN运算符按降序返回值

[英]mysql IN operator returns values in descending order

I am trying to run a query off of my mysql database using the IN operator. 我正在尝试使用IN运算符对我的mysql数据库进行查询。

SELECT usernum FROM usergroup WHERE emailmain IN ('blah@blah.com', 'rawr@rawr.com', 'e@e.com')

The query works but instead of outputing the usernum values in the order entered it outputs them in descending order. 该查询有效,但不是按输入的顺序输出usernum值,而是按降序输出它们。 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 . 我认为这可能与limit运算符或使用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! 任何帮助,将不胜感激!

You can use ORDER BY FIELD to define your order 您可以使用ORDER BY FIELD来定义您的订单

SELECT usernum 
FROM usergroup 
WHERE emailmain IN ('blah@blah.com', 'rawr@rawr.com', 'e@e.com')
ORDER BY FIELD (emailmain, 'blah@blah.com', 'rawr@rawr.com', 'e@e.com')

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. IN完全不会影响结果的顺序,没有ORDER子句,行将以找到的顺序返回。

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 ('blah@blah.com', 'rawr@rawr.com', 'e@e.com')
  ORDER BY FIND_IN_SET(emailmain, "blah@blah.com,rawr@rawr.com,e@e.com")

FIND_IN_SET will return the position of the given string in a comma-delimited list FIND_IN_SET将返回给定字符串在逗号分隔列表中的位置

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. 对于较大的列表,尽管仅使用SELECT usernum, emailmain FROM ...来返回每个结果中的两个字段并将它们与应用程序端相关SELECT usernum, emailmain FROM ...可能会更安全。

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: 如果要动态创建IN字符串,则不必每次运行查询时都创建一个表-您可以使用内联视图,其填充方式与IN字符串相同,如下所示:

select v.email, u.usernum
from 
(select 'blah@blah.com' email union select 'rawr@rawr.com' union select 'e@e.com') v
left join usergroup u on v.email = u.emailmain
order by v.email

为什么不从数据库中选择用户名和电子邮件?

SELECT usernum, emailmain FROM usergroup WHERE emailmain IN ('blah@blah.com', 'rawr@rawr.com', 'e@e.com') ORDER BY emailmain DESC

Providing IN condition does not imply the order. 提供IN条件并不表示订单。

To order your result, use ORDER BY at the end of your query. 要对结果进行排序,请在查询末尾使用ORDER BY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM