12

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.

user1135693
  • 137
  • 1
  • 1
  • 5

8 Answers8

28

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
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • @user1135693 - Can you post your query and results? Either there's something wrong with the query or you've stumbled upon a bug. – Lieven Keersmaekers Jan 30 '12 at 11:39
  • my query SELECT title,project_index FROM projectdetail WHERE project_index BETWEEN 1 AND 6 ORDER BY title ASC,project_index ASC; – user1135693 Jan 30 '12 at 11:40
  • title asc its coming BUT project index it has to show 1 to 6 but it shwoing 6 to 1 – user1135693 Jan 30 '12 at 11:41
  • @user1135693 - and your (relevant) results? – Lieven Keersmaekers Jan 30 '12 at 11:42
  • title-->apple,cat,doll,elep project_index-->6,5,4,3,2,1 like results displaying but i want project_index-->1,2,3,4,5,6 – user1135693 Jan 30 '12 at 12:01
  • 1
    @user1135693 - If `apple` has multiple record with different project indexes, this will put the lowest project index first. If you want all the project index (1) records first, regardles of name, you need to order by project_index first... (`ORDER BY prroject_index, title`) – MatBailie Jan 30 '12 at 12:07
4

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;

Ivaylo Petrov
  • 1,162
  • 9
  • 18
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;

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

IRSHAD
  • 2,855
  • 30
  • 39
0

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
php
  • 4,307
  • 1
  • 24
  • 13
0

Try this:

SELECT title, project_index 
FROM projectdetail  
WHERE project_index BETWEEN 1 AND 6 
ORDER BY project_index, title;
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
-1

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
Taryn
  • 242,637
  • 56
  • 362
  • 405
user3115056
  • 1,266
  • 1
  • 10
  • 25
-1

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.

Ashish Patel
  • 1,011
  • 13
  • 27
-1

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.

Aafaq Ahmad
  • 301
  • 3
  • 13