搜索

首页  >  问答  >  正文

多列升序排序的MySQL实现方法

<p>我正在尝试按升序运行此查询:</p> <pre class="brush:php;toolbar:false;">SELECT title,project_index FROM projectdetail WHERE project_index BETWEEN 1 AND 6 ORDER BY title, project_index ASC;</pre> <p>我需要两列按升序排列,但上述查询只返回一个列按<code>ASC</code>顺序的结果。</p>
P粉244730625P粉244730625461 天前477

全部回复(1)我来回复

  • P粉808697471

    P粉8086974712023-08-23 13:48:18

    升序是大多数(如果不是全部)DBMS的默认排序方式,所以你的语句在这方面有点奇怪,但是无论如何,你可以通过在每个列上添加ASC或DESC来指定排序方式。

    你的语句将变为:

    SELECT  title
            , project_index 
    FROM    projectdetail 
    WHERE   project_index BETWEEN 1 AND 6 
    ORDER BY 
            title ASC
            , project_index ASC

    编辑

    正如@Arvo和@Dems所提到的,你当前是首先按照标题排序,如果标题相同,则按照project_index排序。如果你想要project_index首先排序,你必须将它放在ORDER BY子句的第一位。

    你的语句将变为:

    SELECT  title
            , project_index 
    FROM    projectdetail 
    WHERE   project_index BETWEEN 1 AND 6 
    ORDER BY 
            project_index ASC
            , title ASC

    因为ASC是默认的排序顺序,所以你可以省略它们:

    SELECT  title
            , project_index 
    FROM    projectdetail 
    WHERE   project_index BETWEEN 1 AND 6 
    ORDER BY 
            project_index
            , title

    回复
    0
  • 取消回复