• <legend id='8XrZu'><style id='8XrZu'><dir id='8XrZu'><q id='8XrZu'></q></dir></style></legend>

          <bdo id='8XrZu'></bdo><ul id='8XrZu'></ul>

        <tfoot id='8XrZu'></tfoot>

      1. <small id='8XrZu'></small><noframes id='8XrZu'>

        <i id='8XrZu'><tr id='8XrZu'><dt id='8XrZu'><q id='8XrZu'><span id='8XrZu'><b id='8XrZu'><form id='8XrZu'><ins id='8XrZu'></ins><ul id='8XrZu'></ul><sub id='8XrZu'></sub></form><legend id='8XrZu'></legend><bdo id='8XrZu'><pre id='8XrZu'><center id='8XrZu'></center></pre></bdo></b><th id='8XrZu'></th></span></q></dt></tr></i><div id='8XrZu'><tfoot id='8XrZu'></tfoot><dl id='8XrZu'><fieldset id='8XrZu'></fieldset></dl></div>

        按 SQL IN() 子句中值的顺序排序

        时间:2023-08-18

          <tbody id='Hli0h'></tbody>
        1. <small id='Hli0h'></small><noframes id='Hli0h'>

        2. <i id='Hli0h'><tr id='Hli0h'><dt id='Hli0h'><q id='Hli0h'><span id='Hli0h'><b id='Hli0h'><form id='Hli0h'><ins id='Hli0h'></ins><ul id='Hli0h'></ul><sub id='Hli0h'></sub></form><legend id='Hli0h'></legend><bdo id='Hli0h'><pre id='Hli0h'><center id='Hli0h'></center></pre></bdo></b><th id='Hli0h'></th></span></q></dt></tr></i><div id='Hli0h'><tfoot id='Hli0h'></tfoot><dl id='Hli0h'><fieldset id='Hli0h'></fieldset></dl></div>
          <tfoot id='Hli0h'></tfoot>
                <bdo id='Hli0h'></bdo><ul id='Hli0h'></ul>
                  <legend id='Hli0h'><style id='Hli0h'><dir id='Hli0h'><q id='Hli0h'></q></dir></style></legend>

                  本文介绍了按 SQL IN() 子句中值的顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想知道是否有(可能是更好的方法)按 IN() 子句中值的顺序排序.

                  问题是我有 2 个查询,一个获取所有 ID,第二个获取所有信息.第一个创建我希望第二个排序的 ID 的顺序.ID 以正确的顺序放在 IN() 子句中.

                  所以它会是这样的(极其简化):

                  SELECT id FROM table1 WHERE ... ORDER BY display_order, nameSELECT name, description, ... WHERE id IN ([id's from first])

                  问题是第二个查询返回的结果与将 ID 放入 IN() 子句中的顺序不同.

                  我发现的一个解决方案是将所有 ID 放入带有自动递增字段的临时表中,然后将其加入第二个查询中.

                  有更好的选择吗?

                  注意:由于第一个查询是由用户"运行的,而第二个查询是在后台进程中运行的,因此无法使用子查询将 2 个查询合并为 1 个查询.>

                  我正在使用 MySQL,但我认为让它记录其他 DB 的选项可能会很有用.

                  解决方案

                  使用 MySQL 的 FIELD() 函数:

                  SELECT 名称、描述、...从 ...WHERE id IN([ids, any order])ORDER BY FIELD(id, [ids in order])

                  FIELD() 将返回与第一个参数相等的第一个参数的索引(第一个参数本身除外).

                  FIELD('a', 'a', 'b', 'c')

                  将返回 1

                  FIELD('a', 'c', 'b', 'a')

                  将返回 3

                  如果您将 id 以相同的顺序粘贴到 IN() 子句和 FIELD() 函数中,这将完全符合您的要求.

                  I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause.

                  The problem is that I have 2 queries, one that gets all of the IDs and the second that retrieves all the information. The first creates the order of the IDs which I want the second to order by. The IDs are put in an IN() clause in the correct order.

                  So it'd be something like (extremely simplified):

                  SELECT id FROM table1 WHERE ... ORDER BY display_order, name
                  
                  SELECT name, description, ... WHERE id IN ([id's from first])
                  

                  The issue is that the second query does not return the results in the same order that the IDs are put into the IN() clause.

                  One solution I have found is to put all of the IDs into a temp table with an auto incrementing field which is then joined into the second query.

                  Is there a better option?

                  Note: As the first query is run "by the user" and the second is run in a background process, there is no way to combine the 2 into 1 query using sub queries.

                  I am using MySQL, but I'm thinking it might be useful to have it noted what options there are for other DBs as well.

                  解决方案

                  Use MySQL's FIELD() function:

                  SELECT name, description, ...
                  FROM ...
                  WHERE id IN([ids, any order])
                  ORDER BY FIELD(id, [ids in order])
                  

                  FIELD() will return the index of the first parameter that is equal to the first parameter (other than the first parameter itself).

                  FIELD('a', 'a', 'b', 'c')

                  will return 1

                  FIELD('a', 'c', 'b', 'a')

                  will return 3

                  This will do exactly what you want if you paste the ids into the IN() clause and the FIELD() function in the same order.

                  这篇关于按 SQL IN() 子句中值的顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SELECT 列表不在 GROUP BY 子句中并且包含非聚合列 下一篇:你如何使用“WITH"?MySQL中的条款?

                  相关文章

                  最新文章

                  1. <small id='7M0F9'></small><noframes id='7M0F9'>

                  2. <legend id='7M0F9'><style id='7M0F9'><dir id='7M0F9'><q id='7M0F9'></q></dir></style></legend>

                    <i id='7M0F9'><tr id='7M0F9'><dt id='7M0F9'><q id='7M0F9'><span id='7M0F9'><b id='7M0F9'><form id='7M0F9'><ins id='7M0F9'></ins><ul id='7M0F9'></ul><sub id='7M0F9'></sub></form><legend id='7M0F9'></legend><bdo id='7M0F9'><pre id='7M0F9'><center id='7M0F9'></center></pre></bdo></b><th id='7M0F9'></th></span></q></dt></tr></i><div id='7M0F9'><tfoot id='7M0F9'></tfoot><dl id='7M0F9'><fieldset id='7M0F9'></fieldset></dl></div>

                      <tfoot id='7M0F9'></tfoot>

                        <bdo id='7M0F9'></bdo><ul id='7M0F9'></ul>