Order by one of the keys of composite primary key

1.4k views Asked by At

I have a table with columns named like,

    month    year    id    updated_by
-------------------------------------------
    02     2012     1       'entity1'
    03     2012     2        'e2'

where composite keys are month, year and id. Now I would like to sort by id in the ascending order.

But using order by id does not yield a sorted resultset by id.

What am I missing here? Would be glad for any help directing me towards the answer.

Edit: I am using MySQL 5.0

1

There are 1 answers

1
Gordon Linoff On BEST ANSWER

This is too long for a comment.

You are saying that the query:

select *
from t
order by id

is not ordering the table by id. If so, this would indicate a bug in MySQL, which is unlikely.

Some thoughts:

(1) Perhaps you are doing this in a subquery. The ordering is not guaranteed when done in a subquery.

(2) Perhaps you are expecting an ordering on id to order by the elements of the composite index. That would be an incorrect expectation. Order by the things you want to order on.

(3) Perhaps id is being stored as a character string, but contains only digits. If so, then '10' will come before '9'.

Can you show what query you are using and the results that you are getting?