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
지금이 바로 새 출발점. 인생이란 하루하루가 훈련이다. 우리 자신을 훈련하는 터전이다. 실패도 할 수 있는 훈련장이다. 살아있음이 흥겨운 훈련장이다. 지금 이 행복을 기뻐하지 않고 언제 어디서 행복해지랴. 이 기쁨을 발판삼아 온 힘으로 나아가자. 나의 미래는 지금 이 순간 이 곳에 있다. 지금 여기서 노력하지 않고, 언제 어디서 노력하랴. (오히라 미쓰요의 <그러니까 당신도 살아> 중에서)