Question

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

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top