避免按 MYSQL IN 关键字排序

发布于 2024-10-02 22:47:06 字数 196 浏览 3 评论 0原文

当在数据库中查询一组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生寂 2024-10-09 22:47:06

如果您想按照 in 子句 中指定的顺序按 id 对结果进行排序,您可以使用 FIND_IN_SET 作为:

SELECT id ,title, date 
FROM Table 
WHERE id in (7,1,5,9,3)
ORDER BY FIND_IN_SET(id,'7,1,5,9,3')

If you want to order your result by id in the order specified in the in clause you can make use of FIND_IN_SET as:

SELECT id ,title, date 
FROM Table 
WHERE id in (7,1,5,9,3)
ORDER BY FIND_IN_SET(id,'7,1,5,9,3')
芯好空 2024-10-09 22:47:06

没有进行自动排序或默认排序。您看到的排序很可能是表中行的自然排序,即。它们的插入顺序。如果您希望结果以其他方式排序,请使用 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 an IN clause.

注定孤独终老 2024-10-09 22:47:06

SQL中的WHERE子句不影响排序顺序; ORDER BY 子句可以做到这一点。

如果您没有使用 ORDER BY 指定排序顺序,SQL 将选择自己的顺序,通常是主键的顺序,但也可以是任何顺序。

如果您希望记录按特定顺序排列,则需要指定 ORDER BY 子句来告诉 SQL 您想要的顺序。

如果您想要的顺序仅基于 ID 的奇数序列,那么您需要在 ORDER BY 子句中指定。准确地指定这一点将很棘手。这是可能的,但需要一些尴尬的 SQL 代码,并且会显着减慢查询速度(因为它不再使用键来查找记录)。

如果您想要的 ID 序列是由于其他一些更可预测的因素(例如,您实际上希望按字母名称顺序排列记录),您可以只执行 ORDER BY name (或任何字段是)。

如果您确实想按任意顺序按 ID 排序,则可能需要生成一个可用于排序的临时字段:

SELECT *,
CASE id
  WHEN 7 THEN 1
  WHEN 1 THEN 2
  WHEN 5 THEN 3
  WHEN 3 THEN 4
  WHEN 9 THEN 5
END AS mysortorder
FROM mytable
WHERE id in (7,1,5,9,3)
ORDER BY mysortorder;

The WHERE clause in SQL does not affect the sort order; the ORDER 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:

SELECT *,
CASE id
  WHEN 7 THEN 1
  WHEN 1 THEN 2
  WHEN 5 THEN 3
  WHEN 3 THEN 4
  WHEN 9 THEN 5
END AS mysortorder
FROM mytable
WHERE id in (7,1,5,9,3)
ORDER BY mysortorder;
剩余の解释 2024-10-09 22:47:06

您看到的行为是查询优化的结果,我希望您在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文