MySQL multiple column asc order

拈花ヽ惹草 提交于 2019-12-02 20:57:05
Lieven Keersmaekers

Ascending order is the default for most (if not all) DBMS's so your statement is kind of weird in that respect but nevertheless, you can specify an order for each individual column by adding the specifier ASC or DESC to it.

Your statement then would become

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

Edit

As been mentioned by @Arvo & @Dems, currently you are sorting first on title and for identical titles on project_index. If you want your project_index sorted first, you have to place it first in the ORDER BY clause.

Your statement then becomes

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

and because ASC is the default sort order, you can omit them alltogether

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

If you are using mysql, check this out.

As they say there, you can use SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;

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;

AND you can add more columns like ORDER BY col1 ASC, col2 ASC, col3 DESC;

Try this:

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

You try to sort both columns in ascending order. In mysql, you can use multiple order in a query. But the preference for the order by is very important here. First one get the most preference and next one get second preference.
That means, Your query is

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

Where, order by title got first preference. The mysql will order the 'title' column in ascending order at first and display the result. Then only it will order 'project_index' column. So you cann't get answer as you want.

user3115056

You can try with the below and check--

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

As per your requirement/query i think it is impossible to do ordering more than 2 columns in same table. If you want to order based on value you can do like this.

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

In above query it will first show all title in ascending order except state=2 and then shows all records with state=2 in last.

Use

    ORDER BY title ASC,project_index ASC 

instead of

    ORDER BY title, project_index ASC;

give the order separately for both then it will work properly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!