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

    1. <tfoot id='GJI0M'></tfoot>

      <small id='GJI0M'></small><noframes id='GJI0M'>

    2. MYSQL ORDER BY CASE 问题

      MYSQL ORDER BY CASE Issue(MYSQL ORDER BY CASE 问题)
        <bdo id='82IKx'></bdo><ul id='82IKx'></ul>

        <small id='82IKx'></small><noframes id='82IKx'>

        <tfoot id='82IKx'></tfoot>

      • <legend id='82IKx'><style id='82IKx'><dir id='82IKx'><q id='82IKx'></q></dir></style></legend>

            <tbody id='82IKx'></tbody>

                <i id='82IKx'><tr id='82IKx'><dt id='82IKx'><q id='82IKx'><span id='82IKx'><b id='82IKx'><form id='82IKx'><ins id='82IKx'></ins><ul id='82IKx'></ul><sub id='82IKx'></sub></form><legend id='82IKx'></legend><bdo id='82IKx'><pre id='82IKx'><center id='82IKx'></center></pre></bdo></b><th id='82IKx'></th></span></q></dt></tr></i><div id='82IKx'><tfoot id='82IKx'></tfoot><dl id='82IKx'><fieldset id='82IKx'></fieldset></dl></div>
              • 本文介绍了MYSQL ORDER BY CASE 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我有一个这样的数据库:

                I have a database like this:

                -------------------------------------------------------------------
                |   id_one   |   id_two   |   timestamp_one   |   timestamp_two   |
                -------------------------------------------------------------------
                |     27     |     35     |        9:30       |        NULL       |
                -------------------------------------------------------------------
                |     35     |     27     |        NULL       |        9:35       |
                -------------------------------------------------------------------
                |     27     |     35     |        9:34       |        NULL       |
                -------------------------------------------------------------------
                |     35     |     27     |        NULL       |        9:33       |
                -------------------------------------------------------------------
                

                我需要拉所有 4 行

                ORDER BY 'timestamp_one' if 'id_one'=27 or 
                ORDER BY 'timestamp_two' if 'id_one'=27
                

                这是我现在的声明:

                SELECT * FROM tablename 
                WHERE id_one=27 OR id_two=27 
                ORDER BY 
                  CASE WHEN id_one=27 THEN timestamp_one END DESC, 
                  CASE WHEN id_two=27 THEN timestamp_two END DESC
                

                这很有效,因为输出如下:

                This works good in that is outputs this:

                -------------------------------------------------------------------
                |   id_one   |   id_two   |   timestamp_one   |   timestamp_two   |
                -------------------------------------------------------------------
                |     27     |     35     |        9:30       |        NULL       |
                -------------------------------------------------------------------
                |     27     |     35     |        9:34       |        NULL       |
                -------------------------------------------------------------------
                |     35     |     27     |        NULL       |        9:33       |
                -------------------------------------------------------------------
                |     35     |     27     |        NULL       |        9:35       |
                -------------------------------------------------------------------
                

                但我需要两个时间戳列来排序,就像它们是一列一样,所以它会这样排序:

                But I need to two timestamp columns to order like they are one so it would order like this:

                -------------------------------------------------------------------
                |   id_one   |   id_two   |   timestamp_one   |   timestamp_two   |
                -------------------------------------------------------------------
                |     27     |     35     |        9:30       |        NULL       |
                -------------------------------------------------------------------
                |     35     |     27     |        NULL       |        9:33       |
                -------------------------------------------------------------------
                |     27     |     35     |        9:34       |        NULL       |
                -------------------------------------------------------------------
                |     35     |     27     |        NULL       |        9:35       |
                -------------------------------------------------------------------
                

                我希望这是有道理的.本质上,我试图有两个特定于 WHERE 条件的 ORDER BY 列.然后,一旦为该行选择了正确的 ORDER BY 列,它就会按整个时间戳对 ROWS 进行排序.

                I hope this makes sense. Essentially, I am trying to have two ORDER BY columns that are specific to a WHERE condition. Then once the correct ORDER BY column is chosen for that row, it orders the ROWS by the timestamp as a whole.

                推荐答案

                SELECT id_one, id_two, timestamp_one, timestamp_two      
                FROM tablename 
                WHERE id_one = 27 
                    OR id_two = 27 
                ORDER BY 
                    CASE 
                        WHEN id_one=27 THEN timestamp_one 
                        WHEN id_two=27 THEN timestamp_two 
                    END DESC 
                

                这篇关于MYSQL ORDER BY CASE 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

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

                      <tbody id='j63bn'></tbody>

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

                        <bdo id='j63bn'></bdo><ul id='j63bn'></ul>