避免按 MYSQL IN 关键字排序
当在数据库中查询一组 ids 时,mysql 不会按照指定 ids 的顺序提供结果。我使用的查询如下:
SELECT id ,title, date FROM Table WHERE id in (7,1,5,9,3)
返回的结果按 1,3,5,7,9 的顺序排列。
我怎样才能避免这种自动排序
When querying the db for a set of ids, mysql doesnot provide the results in the order by which the ids were specified. The query i am using is the following:
SELECT id ,title, date FROM Table WHERE id in (7,1,5,9,3)
in return the result provided is in the order 1,3,5,7,9.
How can i avoid this auto sorting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想按照
in 子句
中指定的顺序按id
对结果进行排序,您可以使用FIND_IN_SET
作为:If you want to order your result by
id
in the order specified in thein clause
you can make use ofFIND_IN_SET
as:没有进行自动排序或默认排序。您看到的排序很可能是表中行的自然排序,即。它们的插入顺序。如果您希望结果以其他方式排序,请使用
ORDER BY
子句指定。 SQL 中无法指定排序顺序应遵循IN
子句中项目的顺序。There is no auto-sorting or default sorting going on. The sorting you're seeing is most likely the natural sorting of rows within the table, ie. the order they were inserted. If you want the results sorted in some other way, specify it using an
ORDER BY
clause. There is no way in SQL to specify that a sort order should follow the ordering of items in anIN
clause.SQL中的
WHERE
子句不影响排序顺序;ORDER BY
子句可以做到这一点。如果您没有使用 ORDER BY 指定排序顺序,SQL 将选择自己的顺序,通常是主键的顺序,但也可以是任何顺序。
如果您希望记录按特定顺序排列,则需要指定 ORDER BY 子句来告诉 SQL 您想要的顺序。
如果您想要的顺序仅基于 ID 的奇数序列,那么您需要在
ORDER BY
子句中指定。准确地指定这一点将很棘手。这是可能的,但需要一些尴尬的 SQL 代码,并且会显着减慢查询速度(因为它不再使用键来查找记录)。如果您想要的 ID 序列是由于其他一些更可预测的因素(例如,您实际上希望按字母名称顺序排列记录),您可以只执行
ORDER BY name
(或任何字段是)。如果您确实想按任意顺序按 ID 排序,则可能需要生成一个可用于排序的临时字段:
The
WHERE
clause in SQL does not affect the sort order; theORDER BY
clause does that.If you don't specify a sort order using
ORDER BY
, SQL will pick its own order, which will typically be the order of the primary key, but could be anything.If you want the records in a particular order, you need to specify an
ORDER BY
clause that tells SQL the order you want.If the order you want is based solely on that odd sequence of IDs, then you'd need to specify that in the
ORDER BY
clause. It will be tricky to specify exactly that. It is possible, but will need some awkward SQL code, and will slow down the query significantly (due to it no longer using a key to find the records).If your desired ID sequence is because of some other factor that is more predictable (say for example, you actually want the records in alphabetical name order), you can just do
ORDER BY name
(or whatever the field is).If you really want to sort by the ID in an arbitrary sequence, you may need to generate a temporary field which you can use to sort by:
您看到的行为是查询优化的结果,我希望您在 id 上有一个索引,以便 IN 语句将使用该索引以最有效的方式返回记录。由于尚未指定 ORDER BY 语句,数据库将假定返回记录的顺序并不重要,并将优化速度。 (查看“EXPLAIN SELECT”)
CodeAddicts 或 Spudley 的答案将给出您想要的结果。另一种方法是为“mytable”(或另一个表)中的 id 分配优先级,并使用它来根据需要对记录进行排序。
The behaviour you are seeing is a result of query optimisation, I expect that you have an index on id so that the IN statement will use the index to return records in the most efficient way. As an ORDER BY statement has not been specified the database will assume that the order of the return records is not important and will optimise for speed. (Checkout "EXPLAIN SELECT")
CodeAddicts or Spudley's answer will give the result you want. An alternative is assigning a priority to the id's in "mytable" (or another table) and using this to order the records as desired.