• <tfoot id='kuG4c'></tfoot><legend id='kuG4c'><style id='kuG4c'><dir id='kuG4c'><q id='kuG4c'></q></dir></style></legend>
  • <small id='kuG4c'></small><noframes id='kuG4c'>

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

        在 MySQL“IN"中维护秩序询问

        时间:2023-06-02

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

        • <small id='WhZPv'></small><noframes id='WhZPv'>

              <tbody id='WhZPv'></tbody>

                • <bdo id='WhZPv'></bdo><ul id='WhZPv'></ul>

                • 本文介绍了在 MySQL“IN"中维护秩序询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有下表

                  DROP TABLE IF EXISTS `test`.`foo`;
                  CREATE TABLE  `test`.`foo` (
                    `id` int(10) unsigned NOT NULL auto_increment,
                    `name` varchar(45) NOT NULL,
                    PRIMARY KEY  (`id`)
                  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                  

                  然后我尝试根据主键获取记录

                  Then I try to get records based on the primary key

                  SELECT * FROM foo f where f.id IN (2, 3, 1);
                  

                  然后我得到以下结果

                  +----+--------+
                  | id | name   |
                  +----+--------+
                  |  1 | first  |
                  |  2 | second |
                  |  3 | third  |
                  +----+--------+
                  3 rows in set (0.00 sec)
                  

                  可以看出,结果是按 id 排序的.我想要实现的是按照我在查询中提供的顺序获得排序的结果.鉴于这个例子,它应该返回

                  As one can see, the result is ordered by id. What I'm trying to achieve is to get the results ordered in the sequence I'm providing in the query. Given this example it should return

                  +----+--------+
                  | id | name   |
                  +----+--------+
                  |  2 | second |
                  |  3 | third  |
                  |  1 | first  |
                  +----+--------+
                  3 rows in set (0.00 sec)
                  

                  推荐答案

                  正如另一个答案所提到的:您发布的查询没有关于您希望结果的顺序,只是您希望获得的结果.

                  As the other answer mentions: the query you posted has nothing about what order you'd like your results, just which results you'd like to get.

                  要对结果进行排序,我将使用 ORDER BY FIELD():

                  To order your results, I would use ORDER BY FIELD():

                  SELECT * FROM foo f where f.id IN (2, 3, 1)
                  ORDER BY FIELD(f.id, 2, 3, 1);
                  

                  FIELD 的参数列表可以是可变长度的.

                  The argument list to FIELD can be variable length.

                  这篇关于在 MySQL“IN"中维护秩序询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何让 MySQL 使用 INDEX 进行视图查询? 下一篇:MySQL 更新连接表

                  相关文章

                • <legend id='ccraO'><style id='ccraO'><dir id='ccraO'><q id='ccraO'></q></dir></style></legend>

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

                      <bdo id='ccraO'></bdo><ul id='ccraO'></ul>
                    1. <small id='ccraO'></small><noframes id='ccraO'>

                      <tfoot id='ccraO'></tfoot>