user862010
user862010

Reputation:

Sorting rows sql

I have a question that seems to be 'simple' solution .. but is still pounding me on the head here, I have a table with the rows

columns: x, y   
1 , 3 
3 , 8 
1 , 2 
3 , 7 

then, sometimes, the ordering will be right, knowing that 'all the results in column X' are different, just that .. it is the same, I want to use the column Y as sort criteria, it is possible?

Upvotes: 2

Views: 2697

Answers (7)

user596075
user596075

Reputation:

select x, y
from [your table]
order by x, y

This is defaulting in ascending order. You can specify

....
order by x, y desc

To order y in descending order.

The lesson here is that you sort by multiple fields in a table, you just need to separate the column names by a comma in your query.

Upvotes: 6

Mithrandir
Mithrandir

Reputation: 25337

This would sort the rows by values in col y (ascending):

SELECT x, y FROM table order by y

Upvotes: 0

René Nyffenegger
René Nyffenegger

Reputation: 40499

select * from <tablename>
 order by x, y

Upvotes: 1

SlavaNov
SlavaNov

Reputation: 2485

SELECT x, y FROM table_name ORDER BY x, y

This will order by X and then by Y (if X is the same for two rows)

Upvotes: 3

mikebabcock
mikebabcock

Reputation: 791

You haven't posted the query you're using. Are you using 'ORDER BY' at all?

SELECT x,y FROM coordinates ORDER BY x,y;

This would sort first by X then by Y values.

Upvotes: 2

Yuck
Yuck

Reputation: 50845

Why not just do:

SELECT x, y
FROM table
ORDER BY x, y;

That sorts by x first, using y as the tie-breaker.

Upvotes: 4

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

You can use order by to specify several fields separated with comma (,).

select x, y from table
order by x, y

Upvotes: 3

Related Questions