Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL multiple column asc order

I am trying to run this query in ascending order:

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

I need two columns in ascending order, but the above query returns results with only one column in ASC order.

like image 418
user1135693 Avatar asked Jan 30 '12 11:01

user1135693


People also ask

How do I sort multiple columns in MySQL?

Summary. Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.

Can we use ORDER BY for 2 columns?

Discussion: If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY . This clause comes at the end of your SQL query.

How insert multiple orders MySQL?

If you want to sort multiple columns specify all the columns by a comma. column1 DESC, column2 ASC; Here, the result is sorted by column1 in descending order first then, result is sorted by column2 in ascending order.

Can you arrange the result set of an SQL query on multiple columns?

If you specify multiple columns, the result set is sorted by the first column and then that sorted result set is sorted by the second column, and so on. The columns that appear in the ORDER BY clause must correspond to either column in the select list or columns defined in the table specified in the FROM clause.


2 Answers

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
like image 158
Lieven Keersmaekers Avatar answered Oct 17 '22 19:10

Lieven Keersmaekers


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;

like image 24
Ivaylo Petrov Avatar answered Oct 17 '22 20:10

Ivaylo Petrov