MySQL多列asc命令

时间:2012-01-30 11:29:11

标签: mysql sql select sql-order-by

我正在尝试按升序运行此查询:

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

我需要按升序排列两列,但上面的查询只返回ASC顺序中只有一列的结果。

9 个答案:

答案 0 :(得分:27)

对于大多数(如果不是全部) DBMS,升序是默认,所以你的陈述在这方面有点怪异但是,您可以通过向其添加说明符ASCDESC来为每个列指定订单。

您的陈述将成为

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

修改

正如@Arvo& amp; @Dems,目前您正在排序title上的第一个以及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

答案 1 :(得分:4)

如果您使用的是mysql,请检查this

正如他们所说,你可以使用SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;

答案 2 :(得分:3)

  

ORDER BY title ASC,project_index ASC;

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

您可以添加更多列,例如ORDER BY col1 ASC, col2 ASC, col3 DESC;

答案 3 :(得分:0)

您尝试按升序对两列进行排序。在mysql中,您可以在查询中使用多个顺序。但是这里对订单的偏好非常重要。第一个得到最喜欢,下一个得到第二个偏好。
这意味着,您的查询是

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

其中,按名称排序获得首选。 mysql将首先按升序排序'title'列并显示结果。然后它只会命令'project_index'列。所以你无法得到你想要的答案。

答案 4 :(得分:0)

试试这个:

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

答案 5 :(得分:-1)

您可以尝试使用以下内容并检查 -

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

答案 6 :(得分:-1)

根据您的要求/查询,我认为不可能在同一个表中订购超过2列。如果您想根据价值订购,可以这样做。

SELECT lat,lon, title, zip, city, state, region,cantone 
FROM company 
WHERE title != '' AND state IN(1,3,4,5,6,7,9,2) 
ORDER BY state=2,title asc

在上面的查询中,除了state = 2之外,它将首先以升序显示所有标题,然后显示最后状态= 2的所有记录。

答案 7 :(得分:-1)

使用

    ORDER BY title ASC,project_index ASC 

而不是

    ORDER BY title, project_index ASC;

单独给出订单,然后它将正常工作。

答案 8 :(得分:-2)

您可以尝试:

SELECT title, project_index FROM projectdetail 
ORDER BY title ASC, project_index DESC