Database

[mysql] SQL order string as number

컨텐츠 정보

본문

I have numbers saved as VARCHAR to a MySQL database. I can not make them INT due to some other depending circumstances.

It is taking them as character not as number while sorting.

In database I have

1 2 3 4 5 6 7 8 9 10...

On my page it shows ordered list like this:

1 10 2 3 4 5 6 7 8 9

How can I make it appear ordered by numbers ascending?



---------------------------------------------------------------------------------


select col from yourtable
order by cast(col as unsigned)

or to force a conversion to number do:

select col from yourtable
order by col * 1

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
남을 비난하는 것만큼 쉬운 일은 없다. 어떤 일이 그릇되었다는 것을 아는 데는 그리 많은 것이 필요하지 않다. 하지만 어떻게 하면 그것을 다시 바르게 할 수 있는가를 아는 데는 남다른 눈썰미가 있어야 한다. (빌 로저스)