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

관련자료

등록된 댓글이 없습니다.
Today's proverb
너의 하루하루가 너를 형성한다. 그리고 머지 않아 한 가정을, 지붕 밑의 온도를 형성할 것이다. 또한 그 온도는 이웃으로 번져 한 사회를 이루게 될 것이다. 이렇게 볼 때 너의 ‘있음’은 절대적인 것이다. 없어도 그만인 존재가 아니라는 말이다. (법정스님)