MySQL ORDER BY problem...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Mark53
Forum Newbie
Posts: 1
Joined: Mon Dec 22, 2003 9:03 am

MySQL ORDER BY problem...

Post by Mark53 »

Hi everyone,
I've run a simple select :

select * from ... order by col2, col1

The result is :

col1 col2 ...
4 1
2 1
7 1
5 2
7 2
8 2
8 3

As you can see, the lines are sorted by col2, but NOT but col1. Why ?

Thanks for your help,
Mark
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

Try this
SELECT * FROM tablename ORDER BY col1 ASC
if you want to sorting order by col1, why are you using col2 in ORDER BY clause?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

igoy wrote:if you want to sorting order by col1, why are you using col2 in ORDER BY clause?
Igoy:

You can sort by more than one column.

For example, sorting by col2, then by col1. If the data in some data in col2 is duplicated, for example, if there were 3 people with the surname jones, MySQL would then order each of the people by col1, their firstname.


Mark53:

Not sure why you are having that problem Mark53, what are the data types of the 2 columns.

Mark
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

Right Bech, I know we can sort by two or more columns. But guess Mark wanted to sort by col1.

anyways... even I was wondering why he is getting that problem.
Post Reply